From 3125e2de9b40a7f84898345775c9290c7dc9d3cc Mon Sep 17 00:00:00 2001 From: Kristian Aspinall Date: Fri, 20 May 2022 10:29:25 +0100 Subject: [PATCH] feat: exported icon button styling to reduce code duplication --- src/components/IconButton/IconButton.tsx | 6 ++- src/components/Toggle/Toggle.stories.tsx | 2 +- src/components/Toggle/Toggle.tsx | 42 +------------------ .../ToggleGroup/ToggleGroup.stories.tsx | 1 + 4 files changed, 8 insertions(+), 43 deletions(-) diff --git a/src/components/IconButton/IconButton.tsx b/src/components/IconButton/IconButton.tsx index bb735ace..34d34e2a 100644 --- a/src/components/IconButton/IconButton.tsx +++ b/src/components/IconButton/IconButton.tsx @@ -26,7 +26,7 @@ export const disabled = { opacity: 0.4, } -export const StyledIconButton = styled(DEFAULT_TAG, { +export const IconButtonStyling = { $$main: '$colors$primary', $$mainHover: '$colors$primaryHighlight', $$contrast: '$colors$primaryContrast', @@ -135,7 +135,9 @@ export const StyledIconButton = styled(DEFAULT_TAG, { destructive: 'false', size: 'default', }, -}) +} + +export const StyledIconButton = styled(DEFAULT_TAG, IconButtonStyling) type IconButtonVariants = VariantProps type ButtonProps = ComponentProps diff --git a/src/components/Toggle/Toggle.stories.tsx b/src/components/Toggle/Toggle.stories.tsx index 0c336ca2..2ba5b5c6 100644 --- a/src/components/Toggle/Toggle.stories.tsx +++ b/src/components/Toggle/Toggle.stories.tsx @@ -11,7 +11,7 @@ export default { const Template: ComponentStory = ({ ...args }) => { return ( - + ) diff --git a/src/components/Toggle/Toggle.tsx b/src/components/Toggle/Toggle.tsx index e0157f08..5cd6f179 100644 --- a/src/components/Toggle/Toggle.tsx +++ b/src/components/Toggle/Toggle.tsx @@ -2,18 +2,18 @@ import * as TogglePrimitive from '@radix-ui/react-toggle' import React, { ComponentProps, ElementRef, forwardRef } from 'react' import type { AsProps, CSSProps, VariantProps } from '../../stitches.config' import { styled } from '../../stitches.config' -import { disabled, hover } from '../IconButton' +import { hover, IconButtonStyling } from '../IconButton' export const selected = { backgroundColor: '$$onBackgroundColor', color: '$$onColor', - $$focusColor: '$$onFocusColor', } const focus = hover // TODO: pressed + hover together not working as intended export const ToggleStyling = { + ...IconButtonStyling, $$main: '$colors$primary', $$mainHover: '$colors$primaryHighlight', $$contrast: '$colors$primaryContrast', @@ -23,45 +23,7 @@ export const ToggleStyling = { $$lowlight: '$colors$defaultLowlight', $$onBackgroundColor: '$color$defaultHighlight', $$onColor: '$$main', - $$focusColor: '$$default', - // Reset - alignItems: 'center', - appearance: 'none', - boxSizing: 'border-box', - display: 'inline-flex', - flexShrink: 0, - justifyContent: 'center', - lineHeight: '$none', - margin: '0', - outline: 'none', - padding: '0', - textDecoration: 'none', - userSelect: 'none', - WebkitTapHighlightColor: 'rgba(0,0,0,0)', - '&::before': { - boxSizing: 'border-box', - }, - '&::after': { - boxSizing: 'border-box', - }, - - // Defaults - fontSize: '$0', - borderRadius: '$default', - cursor: 'pointer', - backgroundColor: 'transparent', - border: 'none', - - // Actions - '@motion': { - transition: 'background 0.5s', - }, - backgroundPosition: 'center', - - '&:hover': hover, - '&:focus': focus, - '&:disabled': disabled, '&[data-state=on]': selected, variants: { diff --git a/src/components/ToggleGroup/ToggleGroup.stories.tsx b/src/components/ToggleGroup/ToggleGroup.stories.tsx index 5b27e1b1..748e5418 100644 --- a/src/components/ToggleGroup/ToggleGroup.stories.tsx +++ b/src/components/ToggleGroup/ToggleGroup.stories.tsx @@ -24,6 +24,7 @@ const Template: Story = ({ ...args }) => { type="single" defaultValue="center" aria-label="Text alignment" + {...args} >