Skip to content

Commit

Permalink
feat: give typography spacing props, add stack component
Browse files Browse the repository at this point in the history
  • Loading branch information
SHolleworth committed Jan 30, 2025
1 parent c80fd67 commit 220437c
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 6 deletions.
97 changes: 97 additions & 0 deletions packages/client/src/client/components/Spacing.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import styled, { css } from "styled-components"

import { Spacing as ThemeSpacing } from "../theme/types"

// add custom css properties here
const customMarginPropNames = ["marginX", "marginY"] as const
const customPaddingPropNames = ["paddingX", "paddingY"] as const

// add existing css properties here
const marginPropNames = [
"margin",
"marginTop",
"marginBottom",
"marginLeft",
"marginRight",
...customMarginPropNames,
] as const

const paddingPropNames = [
"padding",
"paddingTop",
"paddingBottom",
"paddingLeft",
"paddingRight",
...customPaddingPropNames,
] as const

type MarginKeys = (typeof marginPropNames)[number]
type PaddingKeys = (typeof paddingPropNames)[number]

type MarginProps = { [k in MarginKeys]?: ThemeSpacing | number }

type PaddingProps = { [k in PaddingKeys]?: ThemeSpacing | number }
export type SpacingProps = MarginProps & PaddingProps

export const SpacingContainer = styled.div<SpacingProps>`
${({ theme, ...props }) => {
const mapSpacingCss = (prop?: ThemeSpacing | number) =>
prop && (typeof prop === "number" ? prop : theme.newTheme.spacing[prop])
const marginProps = Object.entries(props).filter(
([name]) =>
marginPropNames.includes(name as (typeof marginPropNames)[number]) &&
!customMarginPropNames.includes(
name as (typeof customMarginPropNames)[number],
),
)
const paddingProps = Object.entries(props).filter(
([name]) =>
paddingPropNames.includes(name as (typeof paddingPropNames)[number]) &&
!customPaddingPropNames.includes(
name as (typeof customPaddingPropNames)[number],
),
)
const { marginX, marginY, paddingX, paddingY } = props
const customSpacingStyles = {
...(paddingX &&
Object.fromEntries([
["paddingLeft", mapSpacingCss(paddingX)],
["paddingRight", mapSpacingCss(paddingX)],
])),
...(paddingY &&
Object.fromEntries([
["paddingTop", mapSpacingCss(paddingY)],
["paddingBottom", mapSpacingCss(paddingY)],
])),
...(marginX &&
Object.fromEntries([
["marginLeft", mapSpacingCss(marginX)],
["marginRight", mapSpacingCss(marginX)],
])),
...(marginY &&
Object.fromEntries([
["marginTop", mapSpacingCss(marginY)],
["marginBottom", mapSpacingCss(marginY)],
])),
}
const marginStyles = Object.fromEntries(
marginProps.map(([name, value]) => [name, mapSpacingCss(value)]),
)
const paddingStyles = Object.fromEntries(
paddingProps.map(([name, value]) => [name, mapSpacingCss(value)]),
)
return css({
...customSpacingStyles,
// the more specific styles should overwrite the more general custom styles
...marginStyles,
...paddingStyles,
})
}}
`
40 changes: 40 additions & 0 deletions packages/client/src/client/components/Stack.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import styled, { css, CSSProperties } from "styled-components"

import { Spacing } from "../theme/types"
import { SpacingContainer, SpacingProps } from "./Spacing"

interface StackProps extends SpacingProps {
direction?: CSSProperties["flexDirection"]
wrap?: CSSProperties["flexWrap"]
flow?: CSSProperties["flexFlow"]
justifyContent?: CSSProperties["justifyContent"]
alignItems?: CSSProperties["alignItems"]
alignContent?: CSSProperties["alignContent"]
gap?: Spacing | number
}

export const Stack = styled(SpacingContainer)<StackProps>`
display: flex;
${({
theme,
direction,
wrap,
flow,
justifyContent,
alignItems,
alignContent,
gap,
}) =>
css({
flexDirection: direction,
flexWrap: wrap,
flexFlow: flow,
justifyContent,
alignItems,
alignContent,
...(gap && {
gap: typeof gap === "number" ? gap : theme.newTheme.spacing[gap],
}),
})}
`
13 changes: 7 additions & 6 deletions packages/client/src/client/components/Typography.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import styled from "styled-components"
import styled, { css, CSSProperties } from "styled-components"

import { Color, TextStyles } from "../theme/types"
import { SpacingContainer, SpacingProps } from "./Spacing"

interface TypographyProps {
interface TypographyProps extends SpacingProps {
variant?: TextStyles
color?: Color
allowLineHeight?: boolean
textTransform?: CSSProperties["textTransform"]
}

export const Typography = styled.div<TypographyProps>`
export const Typography = styled(SpacingContainer)<TypographyProps>`
${({ variant, theme }) =>
variant ? theme.newTheme.textStyles[variant] : null}
color: ${({ color, theme }) =>
color ? theme.newTheme.color[color] : "inherit"};
margin-block-end: 0;
${({ allowLineHeight }) =>
allowLineHeight ? undefined : `line-height: normal`};
allowLineHeight ? undefined : `line-height: normal;`}
${({ textTransform }) => css({ textTransform })}
`

0 comments on commit 220437c

Please sign in to comment.