|
| 1 | +import styled from '@emotion/styled'; |
| 2 | +import React from 'react'; |
| 3 | +import { Toggle, ToggleProps } from './Toggle'; |
| 4 | + |
| 5 | +export type StyledToggleProps = Omit<ToggleProps, 'contrast' | 'themeId'> & { |
| 6 | + // colors |
| 7 | + backgroundColor: string; |
| 8 | + buttonBackgroundColor: string; |
| 9 | + checkedBackgroundColor: string; |
| 10 | + checkedButtonBackgroundColor: string; |
| 11 | + checkedHoveredFocusRingColor: string; |
| 12 | + hoveredFocusRingColor: string; |
| 13 | + checkedHoveredFocusRingOpacity?: number; |
| 14 | + |
| 15 | + // sizes |
| 16 | + toggleWidth?: number; |
| 17 | + toggleHeight?: number; |
| 18 | + toggleBorderWidth?: number; |
| 19 | + toggleFocusRingSize?: number; |
| 20 | + toggleLabelTopMargin?: number; |
| 21 | + |
| 22 | + // shape |
| 23 | + toggleBorderRadius?: number; |
| 24 | + toggleButtonBorderRadius?: number; |
| 25 | +}; |
| 26 | + |
| 27 | +const BaseStyledToggle = styled(Toggle, { |
| 28 | + shouldForwardProp: (prop: string) => |
| 29 | + ![ |
| 30 | + 'backgroundColor', |
| 31 | + 'buttonBackgroundColor', |
| 32 | + 'checkedBackgroundColor', |
| 33 | + 'checkedButtonBackgroundColor', |
| 34 | + 'checkedHoveredFocusRingColor', |
| 35 | + 'hoveredFocusRingColor', |
| 36 | + 'checkedHoveredFocusRingOpacity', |
| 37 | + 'toggleWidth', |
| 38 | + 'toggleHeight', |
| 39 | + 'toggleBorderWidth', |
| 40 | + 'toggleFocusRingSize', |
| 41 | + 'toggleLabelTopMargin', |
| 42 | + 'toggleBorderRadius', |
| 43 | + 'toggleButtonBorderRadius', |
| 44 | + ].includes(prop), |
| 45 | +})<StyledToggleProps>` |
| 46 | + ${({ backgroundColor }) => backgroundColor && `--toggle-background-color: ${backgroundColor};`} |
| 47 | + ${({ buttonBackgroundColor }) => |
| 48 | + buttonBackgroundColor && `--toggle-button-background-color: ${buttonBackgroundColor};`} |
| 49 | + ${({ hoveredFocusRingColor }) => |
| 50 | + hoveredFocusRingColor && `--toggle-unchecked-hovered-focus-ring-color: ${hoveredFocusRingColor};`} |
| 51 | + ${({ checkedBackgroundColor }) => |
| 52 | + checkedBackgroundColor && `--toggle-checked-background-color: ${checkedBackgroundColor};`} |
| 53 | + ${({ checkedButtonBackgroundColor }) => |
| 54 | + checkedButtonBackgroundColor && `--toggle-checked-button-background-color: ${checkedButtonBackgroundColor};`} |
| 55 | + ${({ checkedHoveredFocusRingColor }) => |
| 56 | + checkedHoveredFocusRingColor && `--toggle-checked-hovered-focus-ring-color: ${checkedHoveredFocusRingColor};`} |
| 57 | + ${({ checkedHoveredFocusRingOpacity }) => |
| 58 | + checkedHoveredFocusRingOpacity && `--toggle-checked-hovered-focus-ring-opacity: ${checkedHoveredFocusRingOpacity};`} |
| 59 | +
|
| 60 | + ${({ toggleWidth }) => toggleWidth && `--toggle-width: ${toggleWidth}px;`} |
| 61 | + ${({ toggleHeight }) => toggleHeight && `--toggle-height: ${toggleHeight}px;`} |
| 62 | + ${({ toggleBorderWidth }) => toggleBorderWidth && `--toggle-border-width: ${toggleBorderWidth}px;`} |
| 63 | + ${({ toggleFocusRingSize }) => toggleFocusRingSize && `--toggle-focus-ring-size: ${toggleFocusRingSize}px;`} |
| 64 | + ${({ toggleLabelTopMargin }) => toggleLabelTopMargin && `--toggle-label-top-margin: ${toggleLabelTopMargin}px;`} |
| 65 | +
|
| 66 | + ${({ toggleBorderRadius }) => toggleBorderRadius && `--toggle-border-radius: ${toggleBorderRadius}px;`} |
| 67 | + ${({ toggleButtonBorderRadius }) => |
| 68 | + toggleButtonBorderRadius && `--toggle-button-border-radius: ${toggleButtonBorderRadius}px;`} |
| 69 | +`; |
| 70 | + |
| 71 | +/** |
| 72 | + * A styled toggle is an extension of the `Toggle` component |
| 73 | + * that will have a custom background and button color. |
| 74 | + */ |
| 75 | +export const StyledToggle = (props: StyledToggleProps) => <BaseStyledToggle {...props} unthemed />; |
| 76 | + |
| 77 | +StyledToggle.displayName = 'StyledToggle'; |
0 commit comments