Created
January 8, 2024 23:59
-
-
Save sammyjoyce/60e3c4161156025dce1f7e4ed64f4dce to your computer and use it in GitHub Desktop.
StyleX Button example
This file contains hidden or 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
| import { ButtonHTMLAttributes, ReactNode } from 'react'; | |
| import * as stylex from '@stylexjs/stylex'; | |
| const baseStyles = stylex.create({ | |
| base: { | |
| backgroundColor: "#3661DA", | |
| border: "1px solid #3661DA", | |
| color: "#fff", | |
| fontWeight: 600, | |
| fontFamily: "Inter, sans-serif", | |
| borderRadius: "4px", | |
| cursor: "pointer", | |
| margin: "10px 0", | |
| boxShadow: "0px 1px 2px 0px rgba(16, 24, 40, 0.05)", | |
| } | |
| }); | |
| const sizeStyles = stylex.create({ | |
| sm: { fontSize: "0.75rem", padding: "0.375rem 0.75rem" }, | |
| md: { fontSize: "1rem", padding: "0.625rem 1rem" }, | |
| lg: { fontSize: "1.25rem", padding: "0.875rem 1.25rem" }, | |
| xl: { fontSize: "1.5rem", padding: "1.125rem 1.5rem" }, | |
| "2xl": { fontSize: "1.875rem", padding: "1.375rem 1.75rem" }, | |
| }); | |
| const hierarchyStyles = stylex.create({ | |
| Primary: { backgroundColor: "#3661DA", border: "1px solid #3661DA" }, | |
| Secondary: { backgroundColor: "transparent", border: "1px solid #3661DA", color: "#3661DA" }, | |
| Tertiary: { backgroundColor: "transparent", border: "none", color: "#3661DA" }, | |
| Link: { backgroundColor: "transparent", border: "none", color: "#3661DA", boxShadow: "none", textDecoration: "underline" }, | |
| }); | |
| interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> { | |
| children: ReactNode; | |
| size?: keyof typeof sizeStyles; | |
| hierarchy?: keyof typeof hierarchyStyles; | |
| style?: stylex.StyleXStyles, | |
| } | |
| export function Button({ children, size = 'md', hierarchy = 'Primary', style, ...rest }: ButtonProps) { | |
| const baseStyle = baseStyles.base; | |
| const sizeStyle = sizeStyles[size]; | |
| const hierarchyStyle = hierarchyStyles[hierarchy]; | |
| return ( | |
| <button {...stylex.props(baseStyle, sizeStyle, hierarchyStyle, style)} {...rest}> | |
| {children} | |
| </button> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment