Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds size prop to the SegmentedControl #2347

Merged
merged 4 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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/large-trains-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Adds a `size` prop to the SegmentedControl component. Users can choose between 'medium' (default), and 'small'. More sizes can be added when/if we find we need them.
11 changes: 11 additions & 0 deletions docs/content/SegmentedControl.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ const Controlled = () => {
render(Controlled)
```

### Small

```jsx live
<SegmentedControl aria-label="File view" size="small">
<SegmentedControl.Button defaultSelected>Preview</SegmentedControl.Button>
<SegmentedControl.Button>Raw</SegmentedControl.Button>
<SegmentedControl.Button>Blame</SegmentedControl.Button>
</SegmentedControl>
```

### With icons and labels

```jsx live
Expand Down Expand Up @@ -210,6 +220,7 @@ render(Controlled)
type="boolean"
description="Whether the segment is selected. This is used for controlled SegmentedControls, and needs to be updated using the onChange handler on SegmentedControl."
/>
<PropsTableRow name="selected" type="'small' | 'medium'" description="The size of the buttons" />
<PropsTableRow
name="defaultSelected"
type="boolean"
Expand Down
14 changes: 9 additions & 5 deletions src/SegmentedControl/SegmentedControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,24 @@ type SegmentedControlProps = {
fullWidth?: boolean | ResponsiveValue<boolean>
/** The handler that gets called when a segment is selected */
onChange?: (selectedIndex: number) => void
/** The size of the buttons */
size?: 'small' | 'medium'
/** Configure alternative ways to render the control when it gets rendered in tight spaces */
variant?: 'default' | Partial<Record<WidthOnlyViewportRangeKeys, 'hideLabels' | 'dropdown' | 'default'>>
} & SxProp

const getSegmentedControlStyles = (isFullWidth?: boolean) => ({
const getSegmentedControlStyles = (props: {isFullWidth?: boolean; size?: SegmentedControlProps['size']}) => ({
backgroundColor: 'segmentedControl.bg',
borderColor: 'border.default',
borderRadius: 2,
borderStyle: 'solid',
borderWidth: 1,
display: isFullWidth ? 'flex' : 'inline-flex',
height: '32px', // TODO: use primitive `control.medium.size` when it is available
display: props.isFullWidth ? 'flex' : 'inline-flex',
fontSize: props.size === 'small' ? 0 : 1,
height: props.size === 'small' ? '28px' : '32px', // TODO: use primitive `control.{small|medium}.size` when it is available
margin: 0,
padding: 0,
width: isFullWidth ? '100%' : undefined
width: props.isFullWidth ? '100%' : undefined
})

const Root: React.FC<React.PropsWithChildren<SegmentedControlProps>> = ({
Expand All @@ -47,6 +50,7 @@ const Root: React.FC<React.PropsWithChildren<SegmentedControlProps>> = ({
children,
fullWidth,
onChange,
size,
sx: sxProp = {},
variant,
...rest
Expand Down Expand Up @@ -91,7 +95,7 @@ const Root: React.FC<React.PropsWithChildren<SegmentedControlProps>> = ({

return React.isValidElement<SegmentedControlIconButtonProps>(childArg) ? childArg.props['aria-label'] : null
}
const listSx = merge(getSegmentedControlStyles(isFullWidth), sxProp as SxProp)
const listSx = merge(getSegmentedControlStyles({isFullWidth, size}), sxProp as SxProp)

if (!ariaLabel && !ariaLabelledby) {
// eslint-disable-next-line no-console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ exports[`SegmentedControl renders consistently 1`] = `
flex-grow: 1;
margin-top: -1px;
margin-bottom: -1px;
height: 32px;
--separator-color: transparent;
}

Expand Down Expand Up @@ -45,7 +44,6 @@ exports[`SegmentedControl renders consistently 1`] = `
flex-grow: 1;
margin-top: -1px;
margin-bottom: -1px;
height: 32px;
--separator-color: #d0d7de;
}

Expand Down Expand Up @@ -82,7 +80,7 @@ exports[`SegmentedControl renders consistently 1`] = `
color: currentColor;
cursor: pointer;
font-family: inherit;
font-size: 14px;
font-size: inherit;
font-weight: 600;
padding: 0;
height: 100%;
Expand Down Expand Up @@ -145,7 +143,7 @@ exports[`SegmentedControl renders consistently 1`] = `
color: currentColor;
cursor: pointer;
font-family: inherit;
font-size: 14px;
font-size: inherit;
font-weight: 400;
padding: var(--segmented-control-button-bg-inset);
height: 100%;
Expand Down Expand Up @@ -216,7 +214,7 @@ exports[`SegmentedControl renders consistently 1`] = `
color: currentColor;
cursor: pointer;
font-family: inherit;
font-size: 14px;
font-size: inherit;
font-weight: 400;
padding: var(--segmented-control-button-bg-inset);
height: 100%;
Expand Down Expand Up @@ -286,6 +284,7 @@ exports[`SegmentedControl renders consistently 1`] = `
display: -webkit-inline-flex;
display: -ms-inline-flexbox;
display: inline-flex;
font-size: 14px;
height: 32px;
margin: 0;
padding: 0;
Expand Down
12 changes: 12 additions & 0 deletions src/SegmentedControl/examples.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Args = {
fullWidthAtNarrow?: boolean
fullWidthAtRegular?: boolean
fullWidthAtWide?: boolean
size?: 'small' | 'medium'
variantAtNarrow: ResponsiveVariantOptions
variantAtRegular: ResponsiveVariantOptions
variantAtWide: ResponsiveVariantOptions
Expand Down Expand Up @@ -72,6 +73,13 @@ export default {
type: 'boolean'
}
},
size: {
defaultValue: 'medium',
control: {
type: 'radio',
options: ['small', 'medium']
}
},
variantAtNarrow: {
name: 'variant.narrow',
defaultValue: 'default',
Expand Down Expand Up @@ -116,6 +124,7 @@ export const Default = (args: Args) => (
aria-label="File view"
fullWidth={parseFullWidthFromArgs(args)}
variant={parseVariantFromArgs(args)}
size={args.size}
>
<SegmentedControl.Button selected>Preview</SegmentedControl.Button>
<SegmentedControl.Button>Raw</SegmentedControl.Button>
Expand All @@ -135,6 +144,7 @@ export const Controlled = (args: Args) => {
onChange={handleChange}
fullWidth={parseFullWidthFromArgs(args)}
variant={parseVariantFromArgs(args)}
size={args.size}
>
<SegmentedControl.Button selected={selectedIndex === 0}>Preview</SegmentedControl.Button>
<SegmentedControl.Button selected={selectedIndex === 1}>Raw</SegmentedControl.Button>
Expand All @@ -151,6 +161,7 @@ export const WithIconsAndLabels = (args: Args) => (
aria-label="File view"
fullWidth={parseFullWidthFromArgs(args)}
variant={parseVariantFromArgs(args)}
size={args.size}
>
<SegmentedControl.Button selected leadingIcon={EyeIcon}>
Preview
Expand All @@ -169,6 +180,7 @@ export const IconsOnly = (args: Args) => (
aria-label="File view"
fullWidth={parseFullWidthFromArgs(args)}
variant={parseVariantFromArgs(args)}
size={args.size}
>
<SegmentedControl.IconButton selected icon={EyeIcon} aria-label="Preview" />
<SegmentedControl.IconButton icon={FileCodeIcon} aria-label="Raw" />
Expand Down
3 changes: 1 addition & 2 deletions src/SegmentedControl/getSegmentedControlStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const getSegmentedControlButtonStyles = (
color: 'currentColor',
cursor: 'pointer',
fontFamily: 'inherit',
fontSize: 1,
fontSize: 'inherit',
fontWeight: props?.selected ? 'bold' : 'normal',
padding: props?.selected ? 0 : 'var(--segmented-control-button-bg-inset)',
height: '100%',
Expand Down Expand Up @@ -114,7 +114,6 @@ export const getSegmentedControlListItemStyles = () => ({
flexGrow: 1,
marginTop: '-1px',
marginBottom: '-1px',
height: '32px', // TODO: use primitive `control.medium.size` when it is available
':not(:last-child)': borderedSegment,
...directChildLayoutAdjustments
})