Skip to content

Commit

Permalink
feat(theme/Button): creating theming from new DS
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerAPfledderer committed Jun 9, 2023
1 parent 740611b commit 7669ef0
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 109 deletions.
175 changes: 86 additions & 89 deletions src/@chakra-ui/gatsby-plugin/components/Button.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,77 @@
import { defineStyle, defineStyleConfig } from "@chakra-ui/react"
import { buttonDefaultTheme, defineMergeStyles } from "./components.utils"

const {
baseStyle: defaultBaseStyle,
sizes: defaultSizes,
variants: defaultVariants,
defaultProps,
} = buttonDefaultTheme
/**
* This selector over the more specific `& .chakra-button__icon` accounts
* for icons used both as a singleton (in IconButton) and as aside element
* with text. The mention classname is not used for the singleton.
*
* And because the icons `fontSize` is different than the text, the icon size
* needs to stay the same when a singleton.
*/
const ICON_SELECTOR = "& svg"

const baseStyle = defineMergeStyles(defaultBaseStyle, {
fontWeight: "normal",
const getBaseColor = (isSecondary: boolean) =>
!isSecondary ? "primary" : "body"

const baseStyle = defineStyle((props) => ({
borderRadius: "base",
color: getBaseColor(props.isSecondary),
transitionProperty: "common",
transitionDuration: "normal",
whiteSpace: "normal",
_focusVisible: {
outline: "4px solid",
outlineColor: "primaryHover",
outlineOffset: -1,
},
_disabled: {
color: "disabled",
pointerEvents: "none",
},
_hover: {
textDecoration: "none",
boxShadow: "primary",
_disabled: {
boxShadow: "none",
},
color: "primaryHover",
},
_focus: {
boxShadow: "outline",
outline: 0,
p: {
m: 0,
},
})
}))

const disabledStylesSolid = defineStyle({
bg: "disabled",
opacity: 1,
})
const HOVER_BOX_SHADOW = "buttonHover"

const disabledStylesOutline = defineStyle({
color: "disabled",
borderColor: "disabled",
opacity: 1,
const variantSolid = defineStyle({
color: "background",
bg: "primary",
_hover: {
color: "background",
bg: "primaryHover",
boxShadow: HOVER_BOX_SHADOW,
},
_active: {
boxShadow: "none",
},
})

const commonOutline = defineStyle({
const variantOutline = defineStyle({
border: "1px",
color: "text",
bg: "transparent",
borderColor: "text",
_hover: {
color: "primary",
bg: "background",
borderColor: "primary",
_disabled: {
...disabledStylesOutline,
},
boxShadow: HOVER_BOX_SHADOW,
},
_active: {
color: "primary",
bg: "primaryLight",
borderColor: "primary",
},
_focus: {
color: "primary",
borderColor: "background",
_disabled: {
...disabledStylesOutline,
},
},
_disabled: {
...disabledStylesOutline,
boxShadow: "none",
},
})

const variantSolid = defineStyle((props) =>
defineMergeStyles(defaultVariants?.solid(props), {
color: "buttonColor",
bg: "primary",
border: 0,
_hover: {
bg: "primary",
opacity: 0.8,
_disabled: {
...disabledStylesSolid,
},
},
_active: {
bg: "primaryHover",
},
_disabled: {
...disabledStylesSolid,
},
})
)

const variantOutline = defineStyle((props) =>
defineMergeStyles(defaultVariants?.outline(props), commonOutline)
)
const variantGhost = {}

const variantOutlineColor = defineStyle({
...commonOutline,
const variantLink = defineStyle({
color: "primary",
borderColor: "primary",
fontWeight: 700,
textDecor: "underline",
py: 0,
px: 1,
_active: {
color: "primary",
},
})

const variantIcon = defineStyle({
Expand All @@ -109,20 +87,39 @@ const variantIcon = defineStyle({
},
})

const sizes = {
md: {
py: "2 !important",
px: "4 !important",
[ICON_SELECTOR]: {
fontSize: "2xl",
},
},
sm: {
fontSize: "xs",
py: "1.5 !important",
px: "2 !important",
lineHeight: "3xs",
[ICON_SELECTOR]: {
fontSize: "md",
},
},
}

const variants = {
solid: variantSolid,
outline: variantOutline,
ghost: variantGhost,
link: variantLink,
icon: variantIcon,
}

export const Button = defineStyleConfig({
baseStyle,
sizes: defineMergeStyles(defaultSizes, {
md: {
h: "42px",
},
}),
variants: {
...buttonDefaultTheme.variants,
// solid is the default variant used by chakra
solid: variantSolid,
outline: variantOutline,
"outline-color": variantOutlineColor,
icon: variantIcon,
sizes,
variants,
defaultProps: {
size: "md",
variant: "solid",
},
defaultProps,
})
2 changes: 0 additions & 2 deletions src/@chakra-ui/gatsby-plugin/components/components.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const {
Avatar: avatarDefaultTheme,
Badge: badgeDefaultTheme,
Breadcrumb: breadcrumbDefaultTheme,
Button: buttonDefaultTheme,
Checkbox: checkboxDefaultTheme,
CloseButton: closeButtonDefaultTheme,
Code: codeDefaultTheme,
Expand All @@ -33,7 +32,6 @@ export {
avatarDefaultTheme,
badgeDefaultTheme,
breadcrumbDefaultTheme,
buttonDefaultTheme,
checkboxDefaultTheme,
closeButtonDefaultTheme,
codeDefaultTheme,
Expand Down
2 changes: 1 addition & 1 deletion src/@chakra-ui/gatsby-plugin/foundations/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const colors = {
300: "#ffb991",
500: "#ff7324",
600: "#c95d20",
800: "#6a3301",
800: "#352313",
},
red: {
100: "#f7c8c8",
Expand Down
3 changes: 2 additions & 1 deletion src/@chakra-ui/gatsby-plugin/foundations/shadows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ const shadows = {
gridBlueBowShadow: "8px 8px 0px 0px var(--eth-colors-gridBlue)",

// * Part of new DS
// TODO: From current theme. Rename to 'buttonHover'
// TODO: From current theme. Deprecate for 'buttonHover'
primary: "4px 4px 0px 0px var(--eth-colors-primaryLight)",
buttonHover: "4px 4px 0 0 var(--eth-colors-primaryLowContrast)",
tooltip: "0 0 16px var(--eth-colors-tooltipShadow)",
}

Expand Down
112 changes: 97 additions & 15 deletions src/components/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,115 @@
import * as React from "react"
import { HStack, IconButton, ThemingProps, VStack } from "@chakra-ui/react"
import { getThemingArgTypes } from "@chakra-ui/storybook-addon"
import { Meta, StoryObj } from "@storybook/react"
import { MdExpandMore, MdChevronRight } from "react-icons/md"
import Button from "."
import theme from "../../@chakra-ui/gatsby-plugin/theme"

export default {
component: Button,
} as Meta<typeof Button>
type ButtonType = typeof Button

export const Solid: StoryObj<typeof Button> = {
const meta: Meta<ButtonType> = {
title: "Atoms / Forms / Buttons",
component: Button,
args: {
children: "What is Ethereum?",
},
argTypes: {
isSecondary: {
defaultValue: false,
type: "boolean",
name: "Is a secondary color?",
if: { arg: "variant", neq: "solid" },
},
},
}

export const Outline: StoryObj<typeof Button> = {
args: {
children: "More on digital money",
variant: "outline",
export default meta

type Story = StoryObj<ButtonType>

const variants: ThemingProps<"Button">["variant"][] = [
"solid",
"outline",
"ghost",
"link",
]

export const StyleVariants: Story = {
argTypes: {
size: {
//@ts-expect-error
...getThemingArgTypes(theme, "Button")?.size,
defaultValue: "md",
},
},
render: (args) => (
<VStack spacing={4}>
{variants.map((variant, idx) => {
if (args.isSecondary && variant === "solid") return
return <Button key={idx} variant={variant} {...args} />
})}
</VStack>
),
}

export const OutlineColor: StoryObj<typeof Button> = {
args: {
children: "More on digital money",
variant: "outline-color",
export const IconVariants: Story = {
argTypes: {
variant: {
control: "radio",
options: ["solid", "outline", "ghost", "link"],
},
},
render: (args) => (
<HStack>
<VStack>
<Button {...args} />
<Button size="sm" {...args} />
</VStack>
<VStack>
<Button leftIcon={<MdExpandMore />} {...args} />
<Button leftIcon={<MdExpandMore />} size="sm" {...args} />
</VStack>
<VStack>
<Button rightIcon={<MdChevronRight />} {...args} />
<Button rightIcon={<MdChevronRight />} size="sm" {...args} />
</VStack>
<VStack>
<IconButton aria-label="next" icon={<MdChevronRight />} {...args} />
<IconButton
aria-label="next"
icon={<MdChevronRight />}
size="sm"
{...args}
/>
</VStack>
</HStack>
),
}

export const Disabled: StoryObj<typeof Button> = {
export const MultiLineText: Story = {
args: {
children: "I am disabled",
isDisabled: true,
children: "Button label can have two lines",
},
render: (args) => (
<HStack>
<VStack maxW="171px">
<Button variant="outline" isSecondary {...args} />
<Button variant="outline" size="sm" isSecondary {...args} />
</VStack>
<VStack maxW="171px">
<Button {...args} />
<Button size="sm" isSecondary {...args} />
</VStack>
<VStack maxW="180px">
<Button rightIcon={<MdChevronRight />} {...args} />
<Button
rightIcon={<MdChevronRight />}
size="sm"
isSecondary
{...args}
/>
</VStack>
</HStack>
),
}
1 change: 1 addition & 0 deletions src/components/Button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { scrollIntoView } from "../../utils/scrollIntoView"

export interface IProps extends ButtonProps {
toId?: string
isSecondary?: boolean
}

const Button: React.FC<IProps> = ({ toId, children, ...props }) => {
Expand Down
4 changes: 3 additions & 1 deletion src/components/ButtonLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Button, ButtonProps } from "@chakra-ui/react"

import Link, { IBaseProps as ILinkProps } from "./Link"

export interface IProps extends ILinkProps, ButtonProps {}
export interface IProps extends ILinkProps, ButtonProps {
isSecondary?: boolean
}

const ButtonLink: React.FC<IProps> = ({ children, ...props }) => {
return (
Expand Down

0 comments on commit 7669ef0

Please sign in to comment.