Skip to content

Commit

Permalink
Making spaces helper generic
Browse files Browse the repository at this point in the history
  • Loading branch information
corbanbrook committed Dec 18, 2024
1 parent fb633cd commit 4955d24
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
51 changes: 41 additions & 10 deletions src/css/variants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,45 @@ const borderRadius = (side?: Side | Corner) => {
}
}

const spaces = (prefix: string, optionalSpace?: { [key: string]: string }) => {
return Object.keys({ ...vars.space, ...optionalSpace }).reduce<{
type PrefixKeysResult<T> = { [K in keyof T]: string }

const prefixKeys = <T extends { [key: string]: string }>(
prefix: string,
obj: T
): PrefixKeysResult<T> => {
return Object.keys(obj).reduce<{
[key: string]: string
}>((acc, key) => {
acc[key] = `${prefix}-${key}`

return acc
}, {})
}, {}) as PrefixKeysResult<T>
}

const prefixValues = <T extends { [key: string]: string }>(
prefix: string,
obj: T
): T => {
return Object.entries(obj).reduce<{
[key: string]: string
}>((acc, [key, value]) => {
acc[key] = `${prefix}-${value}`

return acc
}, {}) as T
}

const extraSpace = {
const spaces = <T extends { [key: string]: string }>(
prefix: string,
optionalSpace?: T
) => {
return {
...prefixKeys(prefix, vars.space),
...prefixValues(prefix, optionalSpace || ({} as T)),
}
}

const extraSpaces = {
none: '0',
px: '1px',
auto: 'auto',
Expand All @@ -115,12 +143,12 @@ const extraSpace = {

export const boxVariants = cva(undefined, {
variants: {
width: spaces('w', extraSpace),
height: spaces('h', extraSpace),
maxWidth: spaces('max-w', extraSpace),
maxHeight: spaces('max-h', extraSpace),
minWidth: spaces('min-w', extraSpace),
minHeight: spaces('min-h', extraSpace),
width: spaces('w', extraSpaces),
height: spaces('h', extraSpaces),
maxWidth: spaces('max-w', extraSpaces),
maxHeight: spaces('max-h', extraSpaces),
minWidth: spaces('min-w', extraSpaces),
minHeight: spaces('min-h', extraSpaces),

// space
top: spaces('t', { auto: 'auto' }),
Expand Down Expand Up @@ -372,3 +400,6 @@ export const boxVariants = cva(undefined, {
},
},
})

boxVariants({ width: '1' })
boxVariants({ width: 'auto' })
6 changes: 6 additions & 0 deletions tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export default {
positive: '#10b981',
negative: '#ef4444',
},

widtn: {
'fit-content': 'fit-content',
'min-content': 'min-content',
'max-content': 'max-content',
},
},
},
plugins: [],
Expand Down

0 comments on commit 4955d24

Please sign in to comment.