Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
7 changes: 7 additions & 0 deletions .changeset/purple-panthers-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@primer/react': patch
---

Button: Allow leadingIcon, trailingIcon, trailingAction to be overridable with sx

<!-- Changed components: Button -->
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions src/Button/Button.dev.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {SearchIcon, TriangleDownIcon, EyeIcon} from '@primer/octicons-react'
import {SearchIcon, TriangleDownIcon, EyeIcon, IssueClosedIcon} from '@primer/octicons-react'
import React from 'react'
import {Button, IconButton} from '.'
import {default as Text} from '../Text'

export default {
title: 'Components/Button/DevOnly',
Expand Down Expand Up @@ -67,8 +68,11 @@ export const TestSxProp = () => {
>
Red
</Button>
<Button leadingIcon={SearchIcon} variant="invisible" sx={{color: 'firebrick'}}>
Red
<Button variant="invisible" sx={{color: 'firebrick'}}>
Invariant color overridden
</Button>
<Button leadingIcon={IssueClosedIcon} sx={{color: 'done.fg'}}>
<Text sx={{color: 'fg.default'}}>Close issue</Text>
</Button>
<Button
size="small"
Expand Down
16 changes: 10 additions & 6 deletions src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {ButtonProps} from './types'
import {ButtonBase} from './ButtonBase'
import {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'
import {defaultSxProp} from '../utils/defaultSxProp'
import {BetterSystemStyleObject} from '../sx'
import {BetterSystemStyleObject, CSSCustomProperties} from '../sx'

const ButtonComponent = forwardRef(({children, sx: sxProp = defaultSxProp, ...props}, forwardedRef): JSX.Element => {
let sxStyles = sxProp
const style: CSSCustomProperties = {}
const leadingVisual = props.leadingVisual ?? props.leadingIcon
const trailingVisual = props.trailingVisual ?? props.trailingIcon

Expand All @@ -15,10 +16,14 @@ const ButtonComponent = forwardRef(({children, sx: sxProp = defaultSxProp, ...pr

if (sxProp !== null && Object.keys(sxProp).length > 0) {
sxStyles = generateCustomSxProp({block, size, leadingVisual, trailingVisual, trailingAction}, sxProp)

// @ts-ignore sxProp can have color attribute
const {color} = sxProp
if (color) style['--button-color'] = color
}

return (
<ButtonBase ref={forwardedRef} as="button" sx={sxStyles} type="button" {...props}>
<ButtonBase ref={forwardedRef} as="button" sx={sxStyles} style={style} type="button" {...props}>
{children}
</ButtonBase>
)
Expand Down Expand Up @@ -71,11 +76,10 @@ export function generateCustomSxProp(
// Possible data attributes: data-size, data-block, data-no-visuals
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
const block = props.block ? `[data-block="block"]` : ''
const noVisuals =
props.leadingVisual || props.trailingVisual || props.trailingAction ? '' : '[data-no-visuals="true"]'
const noVisuals = props.leadingVisual || props.trailingVisual || props.trailingAction ? '' : '[data-no-visuals]'

// this is custom selector. We need to make sure we add the data attributes to the base css class (& -> &[data-attributename="value"]])
const cssSelector = `&${size}${block}${noVisuals}` // &[data-size="small"][data-block="block"][data-no-visuals="true"]
// this is a custom selector. We need to make sure we add the data attributes to the base css class (& -> &[data-attributename="value"]])
const cssSelector = `&${size}${block}${noVisuals}` // &[data-size="small"][data-block="block"][data-no-visuals]

const customSxProp: {
[key: string]: BetterSystemStyleObject
Expand Down
2 changes: 1 addition & 1 deletion src/Button/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const getVariantStyles = (variant: VariantType = 'default', theme?: Theme
borderColor: 'btn.activeBorder',
},
'[data-component="leadingVisual"], [data-component="trailingVisual"], [data-component="trailingAction"]': {
color: 'fg.muted',
color: `var(--button-color, ${theme?.colors.fg.muted})`,
},
},
primary: {
Expand Down
2 changes: 1 addition & 1 deletion src/sx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type BetterCssProperties = {
}

// Support CSS custom properties in the `sx` prop
type CSSCustomProperties = {
export type CSSCustomProperties = {
[key: `--${string}`]: string | number
}

Expand Down