|
| 1 | +import { |
| 2 | + Children, |
| 3 | + cloneElement, |
| 4 | + isValidElement, |
| 5 | + type HTMLAttributes, |
| 6 | + type MouseEvent, |
| 7 | + type ReactElement, |
| 8 | + type ReactNode, |
| 9 | +} from 'react'; |
| 10 | +import styled, { css } from 'styled-components'; |
| 11 | +import { spacing } from '../../spacing'; |
| 12 | + |
| 13 | +type Orientation = 'horizontal' | 'vertical'; |
| 14 | + |
| 15 | +export type ButtonGroupProps = Omit< |
| 16 | + HTMLAttributes<HTMLDivElement>, |
| 17 | + 'onChange' |
| 18 | +> & { |
| 19 | + /** Lay the buttons out in a row (default) or a column. */ |
| 20 | + orientation?: Orientation; |
| 21 | + /** |
| 22 | + * Value of the selected button. Providing `value` together with `onChange` |
| 23 | + * turns the group into a single-select segmented control: every child must |
| 24 | + * carry a `value`, and the child whose `value` matches is rendered selected. |
| 25 | + * Pass `null` to render no selection. |
| 26 | + */ |
| 27 | + value?: string | null; |
| 28 | + /** Called with the clicked child's `value` when selection is enabled. */ |
| 29 | + onChange?: (value: string) => void; |
| 30 | + /** The buttons to group — typically `Button` from `@scality/core-ui/next`. */ |
| 31 | + children: ReactNode; |
| 32 | +}; |
| 33 | + |
| 34 | +const ButtonGroupContainer = styled.div<{ orientation: Orientation }>` |
| 35 | + display: inline-flex; |
| 36 | + flex-direction: ${(props) => |
| 37 | + props.orientation === 'vertical' ? 'column' : 'row'}; |
| 38 | + align-items: stretch; |
| 39 | + border: ${spacing.r1} solid ${(props) => props.theme.border}; |
| 40 | + border-radius: ${spacing.r4}; |
| 41 | + overflow: hidden; |
| 42 | +
|
| 43 | + /* Each child button is wrapped (by Button's Tooltip) in extra elements. |
| 44 | + Let those wrappers and the button itself fill their cell so a vertical |
| 45 | + group reads as equal-width rows; horizontal cells keep their natural |
| 46 | + width since the group never stretches them on the main axis. */ |
| 47 | + & > * { |
| 48 | + display: flex; |
| 49 | + } |
| 50 | + & > * > * { |
| 51 | + flex: 1; |
| 52 | + display: flex; |
| 53 | + } |
| 54 | +
|
| 55 | + /* Strip each child button of its own framing so the group reads as one |
| 56 | + segmented control rather than a row of separate buttons. */ |
| 57 | + .sc-button { |
| 58 | + flex: 1; |
| 59 | + border: none; |
| 60 | + border-radius: 0; |
| 61 | + background: transparent; |
| 62 | + box-shadow: none; |
| 63 | + color: ${(props) => props.theme.textSecondary}; |
| 64 | + } |
| 65 | +
|
| 66 | + /* A single hairline separator between adjacent buttons. */ |
| 67 | + ${(props) => |
| 68 | + props.orientation === 'vertical' |
| 69 | + ? css` |
| 70 | + & > *:not(:last-child) .sc-button { |
| 71 | + border-bottom: ${spacing.r1} solid ${props.theme.border}; |
| 72 | + } |
| 73 | + ` |
| 74 | + : css` |
| 75 | + & > *:not(:last-child) .sc-button { |
| 76 | + border-right: ${spacing.r1} solid ${props.theme.border}; |
| 77 | + } |
| 78 | + `} |
| 79 | +
|
| 80 | + /* Doubled class selector keeps these states above Button's own variant |
| 81 | + rules without resorting to !important. */ |
| 82 | + && .sc-button:hover:enabled { |
| 83 | + background: ${(props) => props.theme.backgroundLevel1}; |
| 84 | + color: ${(props) => props.theme.textPrimary}; |
| 85 | + } |
| 86 | +
|
| 87 | + && .sc-button[aria-pressed='true'] { |
| 88 | + background: ${(props) => props.theme.backgroundLevel1}; |
| 89 | + color: ${(props) => props.theme.textPrimary}; |
| 90 | + box-shadow: inset 0 0 0 ${spacing.r1} ${(props) => props.theme.selectedActive}; |
| 91 | + } |
| 92 | +`; |
| 93 | + |
| 94 | +type SelectableChildProps = { |
| 95 | + value?: string; |
| 96 | + onClick?: (event: MouseEvent<HTMLButtonElement>) => void; |
| 97 | +}; |
| 98 | + |
| 99 | +function ButtonGroup({ |
| 100 | + orientation = 'horizontal', |
| 101 | + value, |
| 102 | + onChange, |
| 103 | + children, |
| 104 | + ...rest |
| 105 | +}: ButtonGroupProps) { |
| 106 | + const selectable = typeof onChange === 'function'; |
| 107 | + |
| 108 | + const items = selectable |
| 109 | + ? Children.map(children, (child) => { |
| 110 | + if (!isValidElement(child)) { |
| 111 | + return child; |
| 112 | + } |
| 113 | + |
| 114 | + const element = child as ReactElement<SelectableChildProps>; |
| 115 | + const childValue = element.props.value; |
| 116 | + const selected = childValue != null && childValue === value; |
| 117 | + |
| 118 | + return cloneElement(element, { |
| 119 | + // Only a child with a `value` is a real toggle button; without one, |
| 120 | + // clicking is a no-op, so it must not advertise a pressed state. |
| 121 | + ...(childValue != null && { 'aria-pressed': selected }), |
| 122 | + onClick: (event: MouseEvent<HTMLButtonElement>) => { |
| 123 | + element.props.onClick?.(event); |
| 124 | + if (childValue != null) { |
| 125 | + onChange(childValue); |
| 126 | + } |
| 127 | + }, |
| 128 | + } as Partial<SelectableChildProps> & { 'aria-pressed'?: boolean }); |
| 129 | + }) |
| 130 | + : children; |
| 131 | + |
| 132 | + return ( |
| 133 | + <ButtonGroupContainer |
| 134 | + className="sc-button-group" |
| 135 | + role="group" |
| 136 | + orientation={orientation} |
| 137 | + {...rest} |
| 138 | + > |
| 139 | + {items} |
| 140 | + </ButtonGroupContainer> |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +export { ButtonGroup }; |
0 commit comments