Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/data-component-override.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/octicons": patch
---

Allow `data-component` attribute to be overridden by consumers
5 changes: 2 additions & 3 deletions lib/octicons_gem/lib/octicons/octicon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ def initialize(symbol, options = {})
@width = octicon["width"]
@height = octicon["height"]
@keywords = octicon["keywords"]
@options = options.dup
@options = { "data-component": "Octicon" }.merge(options.dup)
@options.merge!({
class: classes,
viewBox: viewbox,
version: "1.1",
"data-component": "Octicon"
version: "1.1"
})
@options.merge!(size)
@options.merge!(a11y)
Expand Down
6 changes: 6 additions & 0 deletions lib/octicons_gem/test/octicon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,11 @@
icon = octicon("x")
assert_includes icon.to_svg, "data-component=\"Octicon\""
end

it "allows data-component to be overridden" do
icon = octicon("x", "data-component": "CustomComponent")
assert_includes icon.to_svg, "data-component=\"CustomComponent\""
refute_includes icon.to_svg, "data-component=\"Octicon\""
end
end
end
5 changes: 5 additions & 0 deletions lib/octicons_react/src/__tests__/octicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ describe('An icon component', () => {
expect(container.querySelector('svg')).toHaveAttribute('data-component', 'Octicon')
})

it('allows data-component to be overridden', () => {
const {container} = render(<AlertIcon data-component="CustomComponent" />)
expect(container.querySelector('svg')).toHaveAttribute('data-component', 'CustomComponent')
})

it('sets `role="img"` if `aria-label` is provided', () => {
render(<AlertIcon aria-label="Alert" />)
expect(screen.getByLabelText('Alert')).toHaveAttribute('role', 'img')
Expand Down
2 changes: 1 addition & 1 deletion lib/octicons_react/src/createIconComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
return (
<svg
ref={forwardedRef}
data-component="Octicon"
{...rest}
aria-hidden={labelled ? undefined : 'true'}
Comment thread
francinelucca marked this conversation as resolved.
tabIndex={tabIndex}
Expand All @@ -53,7 +54,6 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
id={id}
display="inline-block"
overflow="visible"
data-component="Octicon"
style={{
verticalAlign,
...style
Expand Down
10 changes: 10 additions & 0 deletions lib/octicons_styled/src/__tests__/octicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ describe('An icon component', () => {
expect(container.querySelector('svg')).toHaveClass('foo')
})

it('has `data-component="Octicon"` attribute by default', () => {
const {container} = render(<AlertIcon />)
expect(container.querySelector('svg')).toHaveAttribute('data-component', 'Octicon')
})

it('allows data-component to be overridden', () => {
const {container} = render(<AlertIcon data-component="CustomComponent" />)
expect(container.querySelector('svg')).toHaveAttribute('data-component', 'CustomComponent')
})

it('respects the verticalAlign prop', () => {
const {container} = render(<AlertIcon verticalAlign="middle" />)
expect(container.querySelector('svg')).toHaveStyle({verticalAlign: 'middle'})
Expand Down
Loading