Skip to content

Commit 2c05542

Browse files
add changeset
1 parent b7ced96 commit 2c05542

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

.changeset/purple-panthers-accept.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
Button: Allow leadingIcon, trailingIcon, trailingAction to be overridable with sx
6+
7+
<!-- Changed components: Button -->

src/Button/Button.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,34 @@ export function generateCustomSxProp(
6666
props: Partial<Pick<ButtonProps, 'size' | 'block' | 'leadingIcon' | 'trailingIcon' | 'trailingAction'>>,
6767
providedSx: BetterSystemStyleObject,
6868
) {
69-
const cssSelectorList = []
7069
// Possible data attributes for button component: data-size, data-block, data-no-visuals
7170
const size = props.size && props.size !== 'medium' ? `[data-size="${props.size}"]` : '' // medium is a default size therefore it doesn't have a data attribute that used for styling
7271
const block = props.block ? `[data-block="block"]` : ''
7372
const noVisuals = props.leadingIcon || props.trailingIcon || props.trailingAction ? '' : '[data-no-visuals="true"]'
7473
// We need to make sure we add the data attributes to the base css class (& -> &[data-attributename="value"]]) otherwise sx styles are applied due to data attribute has more specificity than class selector
7574
const cssSelector = `&${size}${block}${noVisuals}` // &[data-size="small"][data-block="block"][data-no-visuals="true"]
76-
cssSelectorList.push(cssSelector)
75+
76+
const customSxProp: {
77+
[key: string]: BetterSystemStyleObject
78+
} = {}
79+
80+
customSxProp[cssSelector] = providedSx as BetterSystemStyleObject & {color?: string}
7781

7882
// Possible data attributes for children of button component: data-component="leadingVisual", data-component="trailingVisual", data-component="trailingAction"
7983
// When sx is used in the parent button component, these styles aren't applied to its children (leadingIcon, trailingIcon, trailingAction)
8084
// Because sx only applies to the base selector which is "&", but we want it to be applied to '& [data-component="leadingVisual"]'
85+
const cssSelectorList = []
8186
if (props.leadingIcon) cssSelectorList.push(`& [data-component="leadingVisual"]`)
8287
if (props.trailingIcon) cssSelectorList.push(`& [data-component="trailingVisual"]`)
8388
if (props.trailingAction) cssSelectorList.push(`& [data-component="trailingAction"]`)
8489

85-
const customSxProp: {
86-
[key: string]: BetterSystemStyleObject
87-
} = {}
88-
89-
if (!providedSx) return customSxProp
90-
else {
91-
for (const selector of cssSelectorList) {
92-
customSxProp[selector] = providedSx
93-
}
94-
return customSxProp
90+
// @ts-ignore - We are sure that color exists in providedSx
91+
const {color} = providedSx
92+
if (!color) return customSxProp
93+
for (const selector of cssSelectorList) {
94+
customSxProp[selector] = {color} // We only want to apply the color to the children of button component, because it is the one that we are overriding with data attributes that has more specificity than the parent button component.
9595
}
96+
return customSxProp
9697
}
9798

9899
ButtonComponent.displayName = 'Button'

0 commit comments

Comments
 (0)