From 4955d24a148d4081b19d520c349a27a11ab088ec Mon Sep 17 00:00:00 2001 From: Corban Riley Date: Wed, 18 Dec 2024 14:36:56 -0500 Subject: [PATCH] Making spaces helper generic --- src/css/variants.ts | 51 ++++++++++++++++++++++++++++++++++++--------- tailwind.config.ts | 6 ++++++ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/css/variants.ts b/src/css/variants.ts index 2707b35f3..c8e5aa1fb 100644 --- a/src/css/variants.ts +++ b/src/css/variants.ts @@ -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 = { [K in keyof T]: string } + +const prefixKeys = ( + prefix: string, + obj: T +): PrefixKeysResult => { + return Object.keys(obj).reduce<{ [key: string]: string }>((acc, key) => { acc[key] = `${prefix}-${key}` return acc - }, {}) + }, {}) as PrefixKeysResult +} + +const prefixValues = ( + 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 = ( + prefix: string, + optionalSpace?: T +) => { + return { + ...prefixKeys(prefix, vars.space), + ...prefixValues(prefix, optionalSpace || ({} as T)), + } +} + +const extraSpaces = { none: '0', px: '1px', auto: 'auto', @@ -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' }), @@ -372,3 +400,6 @@ export const boxVariants = cva(undefined, { }, }, }) + +boxVariants({ width: '1' }) +boxVariants({ width: 'auto' }) diff --git a/tailwind.config.ts b/tailwind.config.ts index c95ba718b..64cfa1bcc 100644 --- a/tailwind.config.ts +++ b/tailwind.config.ts @@ -31,6 +31,12 @@ export default { positive: '#10b981', negative: '#ef4444', }, + + widtn: { + 'fit-content': 'fit-content', + 'min-content': 'min-content', + 'max-content': 'max-content', + }, }, }, plugins: [],