Skip to content

Commit 38dfb0d

Browse files
Chore: make data-component attribute overridable (#1190)
* Make data-component overridable and add tests * add changeset * Add data-component attribute to Octicon and allow overriding in tests
1 parent 31c133c commit 38dfb0d

6 files changed

Lines changed: 29 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/octicons": patch
3+
---
4+
5+
Allow `data-component` attribute to be overridden by consumers

lib/octicons_gem/lib/octicons/octicon.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ def initialize(symbol, options = {})
1313
@width = octicon["width"]
1414
@height = octicon["height"]
1515
@keywords = octicon["keywords"]
16-
@options = options.dup
16+
@options = { "data-component": "Octicon" }.merge(options.dup)
1717
@options.merge!({
1818
class: classes,
1919
viewBox: viewbox,
20-
version: "1.1",
21-
"data-component": "Octicon"
20+
version: "1.1"
2221
})
2322
@options.merge!(size)
2423
@options.merge!(a11y)

lib/octicons_gem/test/octicon_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,11 @@
140140
icon = octicon("x")
141141
assert_includes icon.to_svg, "data-component=\"Octicon\""
142142
end
143+
144+
it "allows data-component to be overridden" do
145+
icon = octicon("x", "data-component": "CustomComponent")
146+
assert_includes icon.to_svg, "data-component=\"CustomComponent\""
147+
refute_includes icon.to_svg, "data-component=\"Octicon\""
148+
end
143149
end
144150
end

lib/octicons_react/src/__tests__/octicon.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ describe('An icon component', () => {
1919
expect(container.querySelector('svg')).toHaveAttribute('data-component', 'Octicon')
2020
})
2121

22+
it('allows data-component to be overridden', () => {
23+
const {container} = render(<AlertIcon data-component="CustomComponent" />)
24+
expect(container.querySelector('svg')).toHaveAttribute('data-component', 'CustomComponent')
25+
})
26+
2227
it('sets `role="img"` if `aria-label` is provided', () => {
2328
render(<AlertIcon aria-label="Alert" />)
2429
expect(screen.getByLabelText('Alert')).toHaveAttribute('role', 'img')

lib/octicons_react/src/createIconComponent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
3838
return (
3939
<svg
4040
ref={forwardedRef}
41+
data-component="Octicon"
4142
{...rest}
4243
aria-hidden={labelled ? undefined : 'true'}
4344
tabIndex={tabIndex}
@@ -53,7 +54,6 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
5354
id={id}
5455
display="inline-block"
5556
overflow="visible"
56-
data-component="Octicon"
5757
style={{
5858
verticalAlign,
5959
...style

lib/octicons_styled/src/__tests__/octicon.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ describe('An icon component', () => {
1717
expect(container.querySelector('svg')).toHaveClass('foo')
1818
})
1919

20+
it('has `data-component="Octicon"` attribute by default', () => {
21+
const {container} = render(<AlertIcon />)
22+
expect(container.querySelector('svg')).toHaveAttribute('data-component', 'Octicon')
23+
})
24+
25+
it('allows data-component to be overridden', () => {
26+
const {container} = render(<AlertIcon data-component="CustomComponent" />)
27+
expect(container.querySelector('svg')).toHaveAttribute('data-component', 'CustomComponent')
28+
})
29+
2030
it('respects the verticalAlign prop', () => {
2131
const {container} = render(<AlertIcon verticalAlign="middle" />)
2232
expect(container.querySelector('svg')).toHaveStyle({verticalAlign: 'middle'})

0 commit comments

Comments
 (0)