-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Button component Includes Primary, Secondary, Tertiary, and Danger buttons * Adds support for Icon within a the Button Adds active state as defined by design for each button Tertiary button has different sizing, that is now accounted for Removed 'fill="none"' from Icon svg to allow for passing the Icon's color prop as "inherit" * Use 'useFocusRing' hook instead of wrapping component * Updates after rebasing * Updates after rebasing. 1 - Reorg files. 2 - Use new truss rules for _Px(). 3-Add border radius rules. 4 - Update Button component to work with new Icon component * Use fill truss prop rather than add('fill',...)
- Loading branch information
Brandon
authored
Mar 15, 2021
1 parent
1ff13f6
commit 5c745cd
Showing
8 changed files
with
1,006 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { Meta } from "@storybook/react"; | ||
import { Button, Css } from "src/index"; | ||
|
||
export default { | ||
component: Button, | ||
title: "Components/Buttons", | ||
} as Meta; | ||
|
||
export function Buttons() { | ||
return ( | ||
<div css={{ ...Css.dg.$, gridTemplateColumns: "repeat(4, auto)" }}> | ||
<div> | ||
<h2>Primary</h2> | ||
<div> | ||
<Button autoFocus>Primary Button</Button> | ||
<Button isDisabled>Disabled</Button> | ||
</div> | ||
<div> | ||
<Button size="md">Primary Button</Button> | ||
<Button size="md" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button size="lg">Primary Button</Button> | ||
<Button size="lg" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button icon="plus">Primary Button</Button> | ||
<Button isDisabled icon="plus"> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button size="md" icon="plus"> | ||
Primary Button | ||
</Button> | ||
<Button size="md" isDisabled icon="plus"> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button size="lg" icon="plus"> | ||
Primary Button | ||
</Button> | ||
<Button size="lg" isDisabled icon="plus"> | ||
Disabled | ||
</Button> | ||
</div> | ||
</div> | ||
|
||
<div> | ||
<h2>Secondary</h2> | ||
<div> | ||
<Button variant="secondary">Secondary Button</Button> | ||
<Button variant="secondary" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button size="md" variant="secondary"> | ||
Secondary Button | ||
</Button> | ||
<Button size="md" variant="secondary" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button size="lg" variant="secondary"> | ||
Secondary Button | ||
</Button> | ||
<Button size="lg" variant="secondary" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button icon="plus" variant="secondary"> | ||
Secondary Button | ||
</Button> | ||
<Button icon="plus" variant="secondary" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button icon="plus" size="md" variant="secondary"> | ||
Secondary Button | ||
</Button> | ||
<Button icon="plus" size="md" variant="secondary" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button icon="plus" size="lg" variant="secondary"> | ||
Secondary Button | ||
</Button> | ||
<Button icon="plus" size="lg" variant="secondary" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
</div> | ||
|
||
<div> | ||
<h2>Tertiary</h2> | ||
<div> | ||
<Button variant="tertiary">Tertiary Button</Button> | ||
<Button variant="tertiary" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button icon="plus" variant="tertiary"> | ||
Tertiary Button | ||
</Button> | ||
<Button icon="plus" variant="tertiary" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
</div> | ||
|
||
<div> | ||
<h2>Danger!</h2> | ||
<div> | ||
<Button variant="danger">Danger Button</Button> | ||
<Button variant="danger" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button size="md" variant="danger"> | ||
Danger Button | ||
</Button> | ||
<Button size="md" variant="danger" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button size="lg" variant="danger"> | ||
Danger Button | ||
</Button> | ||
<Button size="lg" variant="danger" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button icon="trash" variant="danger"> | ||
Danger Button | ||
</Button> | ||
<Button icon="trash" variant="danger" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button icon="trash" size="md" variant="danger"> | ||
Danger Button | ||
</Button> | ||
<Button icon="trash" size="md" variant="danger" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
<div> | ||
<Button icon="trash" size="lg" variant="danger"> | ||
Danger Button | ||
</Button> | ||
<Button icon="trash" size="lg" variant="danger" isDisabled> | ||
Disabled | ||
</Button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import type { AriaButtonProps } from "@react-types/button"; | ||
import { useMemo, useRef } from "react"; | ||
import { useButton, useFocusRing } from "react-aria"; | ||
import { Icon, IconProps } from "src/components/Icon"; | ||
import { Css, Palette } from "src/Css"; | ||
|
||
interface ButtonProps extends AriaButtonProps { | ||
variant?: ButtonVariant; | ||
size?: ButtonSize; | ||
icon?: IconProps["icon"]; | ||
} | ||
|
||
export function Button(props: ButtonProps) { | ||
const { children, icon, variant = "primary", size = "sm" } = props; | ||
const ref = useRef(null); | ||
const { buttonProps } = useButton(props, ref); | ||
const { isFocusVisible, focusProps } = useFocusRing(props); | ||
const buttonStyles = useMemo(() => getButtonStyles(variant, size), [variant, size]); | ||
const focusRingStyles = useMemo(() => (variant === "danger" ? dangerFocusRingStyles : defaultFocusRingStyles), [ | ||
variant, | ||
]); | ||
|
||
return ( | ||
<button | ||
ref={ref} | ||
{...buttonProps} | ||
{...focusProps} | ||
css={{ ...buttonReset, ...buttonStyles, ...(isFocusVisible ? focusRingStyles : {}) }} | ||
> | ||
{icon && <Icon xss={iconStyles[size]} color="inherit" icon={icon} />} | ||
{children} | ||
</button> | ||
); | ||
} | ||
|
||
const buttonReset = Css.p0.bsNone.cursorPointer.smEm.br4.dif.itemsCenter | ||
.mPx(4) | ||
.add("font", "inherit") | ||
.add("boxSizing", "border-box") | ||
.add("outline", "inherit").$; | ||
const disabledStyles = Css.add("cursor", "not-allowed").$; | ||
const defaultFocusRingStyles = Css.add("boxShadow", `0px 0px 0px 2px ${Palette.White}, 0 0 0 4px ${Palette.Sky500}`).$; | ||
const dangerFocusRingStyles = Css.add("boxShadow", `0px 0px 0px 2px ${Palette.White}, 0 0 0 4px ${Palette.Coral600}`).$; | ||
|
||
const variantStyles: Record<ButtonVariant, {}> = { | ||
primary: { | ||
...Css.bgSky500.white.fill(Palette.White).$, | ||
"&:hover:not(:disabled)": Css.bgSky700.$, | ||
"&:disabled": { ...disabledStyles, ...Css.bgSky200.$ }, | ||
"&:active:not(:disabled)": Css.bgSky900.$, | ||
}, | ||
|
||
secondary: { | ||
...Css.bgWhite.bCoolGray300.bw1.ba.coolGray900.fill(Palette.CoolGray900).$, | ||
"&:hover:not(:disabled):not(:active)": Css.bgCoolGray50.$, | ||
"&:disabled": { ...disabledStyles, ...Css.bgWhite.coolGray300.fill(Palette.CoolGray300).$ }, | ||
"&:active:not(:disabled)": Css.bgCoolGray200.$, | ||
}, | ||
|
||
tertiary: { | ||
...Css.add("background", "none").sky500.fill(Palette.Sky500).$, | ||
"&:hover:not(:disabled)": Css.bgCoolGray100.$, | ||
"&:disabled": { ...disabledStyles, ...Css.coolGray300.fill(Palette.CoolGray300).$ }, | ||
"&:active:not(:disabled)": Css.sky900.fill(Palette.Sky900).$, | ||
}, | ||
|
||
danger: { | ||
...Css.bgCoral600.white.fill(Palette.White).$, | ||
"&:hover:not(:disabled)": Css.bgCoral500.$, | ||
"&:disabled": { ...disabledStyles, ...Css.bgCoral200.$ }, | ||
"&:active:not(:disabled)": Css.bgCoral700.$, | ||
}, | ||
}; | ||
|
||
const sizeStyles: Record<ButtonSize, {}> = { | ||
sm: Css.hPx(32).pxPx(12).$, | ||
md: Css.hPx(40).px2.$, | ||
lg: Css.hPx(48).px3.$, | ||
}; | ||
|
||
const iconStyles: Record<ButtonSize, IconProps["xss"]> = { | ||
sm: Css.mrPx(4).$, | ||
md: Css.mr1.$, | ||
lg: Css.mrPx(10).$, | ||
}; | ||
|
||
function getButtonStyles(variant: ButtonVariant, size: ButtonSize) { | ||
// Handling tertiary separately as it only supports a single size button. The size it supports does not match styles of other buttons. | ||
if (variant === "tertiary") { | ||
return { | ||
...Css.hPx(40).px1.$, | ||
...variantStyles.tertiary, | ||
}; | ||
} | ||
|
||
return { | ||
...sizeStyles[size], | ||
...variantStyles[variant], | ||
}; | ||
} | ||
|
||
type ButtonSize = "sm" | "md" | "lg"; | ||
type ButtonVariant = "primary" | "secondary" | "tertiary" | "danger"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './Icon'; | ||
export * from "./Button"; | ||
export * from "./Icon"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './Css'; | ||
export * from './components'; | ||
export * from "./components"; | ||
export * from "./Css"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.