Skip to content

Commit 25e257f

Browse files
Add data-component attribute to Octicon SVGs (#1185)
* Add data-component attribute to Octicon SVGs * fix snapshot * add changeset * fix CI: format node test and update React bundle size snapshot Agent-Logs-Url: https://github.com/primer/octicons/sessions/aa768eed-37b6-4acf-9261-a7deb8fefd68 Co-authored-by: francinelucca <40550942+francinelucca@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 9bc137c commit 25e257f

9 files changed

Lines changed: 27 additions & 3 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@primer/octicons": minor
3+
"@primer/octicons-react": minor
4+
"@primer/styled-octicons": minor
5+
---
6+
7+
Add `data-component="Octicon"` attribute to all SVG elements for easier identification and styling

lib/octicons_gem/lib/octicons/octicon.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def initialize(symbol, options = {})
1717
@options.merge!({
1818
class: classes,
1919
viewBox: viewbox,
20-
version: "1.1"
20+
version: "1.1",
21+
"data-component": "Octicon"
2122
})
2223
@options.merge!(size)
2324
@options.merge!(a11y)

lib/octicons_gem/test/octicon_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,11 @@
134134
assert_includes icon.to_svg, "aria-hidden=\"true\""
135135
end
136136
end
137+
138+
describe "data-component" do
139+
it "has data-component attribute" do
140+
icon = octicon("x")
141+
assert_includes icon.to_svg, "data-component=\"Octicon\""
142+
end
143+
end
137144
end

lib/octicons_node/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ for (const key of Object.keys(data)) {
5555
height: parseInt(height),
5656
viewBox: `0 0 ${data[key].heights[height].width} ${height}`,
5757
class: `octicon octicon-${key}`,
58-
'aria-hidden': 'true'
58+
'aria-hidden': 'true',
59+
'data-component': 'Octicon'
5960
}
6061
}
6162

lib/octicons_node/tests/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ test('Octicons have default html attributes', t => {
2727
new RegExp(`class="octicon octicon-${octicons[point].symbol}"`),
2828
`The octicon "${point}" doesn't have the class attribute`
2929
)
30+
t.regex(svg, /data-component="Octicon"/, `The octicon "${point}" doesn't have the data-component attribute`)
3031
}
3132
})
3233

lib/octicons_react/__tests__/tree-shaking.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ test('tree shaking single export', async () => {
5050
})
5151

5252
const bundleSize = Buffer.byteLength(output[0].code.trim()) / 1000
53-
expect(`${bundleSize}kB`).toMatchInlineSnapshot(`"6.084kB"`)
53+
expect(`${bundleSize}kB`).toMatchInlineSnapshot(`"6.119kB"`)
5454
})

lib/octicons_react/src/__tests__/__snapshots__/octicon.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`An icon component matches snapshot 1`] = `
44
<svg
55
aria-hidden="true"
66
class="octicon octicon-alert"
7+
data-component="Octicon"
78
display="inline-block"
89
fill="currentColor"
910
focusable="false"

lib/octicons_react/src/__tests__/octicon.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ describe('An icon component', () => {
1414
expect(container.querySelector('svg')).toHaveAttribute('aria-hidden', 'true')
1515
})
1616

17+
it('has `data-component="Octicon"` attribute', () => {
18+
const {container} = render(<AlertIcon />)
19+
expect(container.querySelector('svg')).toHaveAttribute('data-component', 'Octicon')
20+
})
21+
1722
it('sets `role="img"` if `aria-label` is provided', () => {
1823
render(<AlertIcon aria-label="Alert" />)
1924
expect(screen.getByLabelText('Alert')).toHaveAttribute('role', 'img')

lib/octicons_react/src/createIconComponent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
5353
id={id}
5454
display="inline-block"
5555
overflow="visible"
56+
data-component="Octicon"
5657
style={{
5758
verticalAlign,
5859
...style

0 commit comments

Comments
 (0)