@telegraph/style-engine 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/cjs/chunk-Dlk1eExg.js +1 -0
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/postcss.js +2 -2
- package/dist/cjs/postcss.js.map +1 -1
- package/dist/esm/index.mjs +402 -148
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/postcss.mjs +105 -140
- package/dist/esm/postcss.mjs.map +1 -1
- package/package.json +11 -11
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../src/helpers/getStyleProp/getStyleProp.ts","../../src/hooks/useStyleEngine.ts"],"sourcesContent":["type Direction =\n | \"x\"\n | \"y\"\n | \"top\"\n | \"bottom\"\n | \"left\"\n | \"right\"\n | \"all\"\n | \"side-top\"\n | \"side-bottom\"\n | \"side-left\"\n | \"side-right\";\n\ntype Axis = \"x\" | \"y\" | \"both\";\n\nexport type CssVarProp = {\n cssVar: string;\n value: string;\n direction?: Direction;\n axis?: Axis;\n};\n\n// Supported pseudo-class states for the object syntax\n// Prefixed with underscore to avoid collisions with native HTML attributes\n// (e.g. `disabled`) and common component props (e.g. `active`).\nexport const PSEUDO_STATES = [\n \"_hover\",\n \"_focus\",\n \"_active\",\n \"_focusWithin\",\n \"_disabled\",\n] as const;\n\nexport type PseudoState = (typeof PSEUDO_STATES)[number];\n\n// Maps pseudo state names to CSS variable prefixes\nconst PSEUDO_CSS_PREFIX: Record<PseudoState, string> = {\n _hover: \"hover\",\n _focus: \"focus\",\n _active: \"active\",\n _focusWithin: \"focus-within\",\n _disabled: \"disabled\",\n};\n\n/**\n * Adds pseudo-class variant props to a style props type.\n *\n * Given a base style props type, this creates a new type that also\n * accepts `_hover`, `_focus`, `_active`, `_focusWithin`, and `_disabled`\n * props as objects containing any of the base style props.\n *\n * Prefixed with underscore to avoid collisions with native HTML attributes\n * (e.g. `disabled`) and common component props (e.g. `active`).\n *\n * @example\n * type BoxStyle = { bg: ColorToken; p: SpacingToken };\n * type BoxStyleWithPseudo = WithPseudo<BoxStyle>;\n * // Allows: { bg: \"gray-2\", _hover: { bg: \"gray-3\" }, _focus: { p: \"4\" } }\n */\nexport type WithPseudo<Props> = Props & {\n _hover?: Partial<Props>;\n _focus?: Partial<Props>;\n _active?: Partial<Props>;\n _focusWithin?: Partial<Props>;\n _disabled?: Partial<Props>;\n};\n\n// Helper type to create negative spacing values\n// This distributes over union types for proper type expansion\nexport type NegativeSpacing<T extends string | number | symbol> =\n T extends string ? `-${T}` : never;\n\n// Type that allows both positive and negative spacing values for a given token type\n// Using direct template literal for better TypeScript inference and autocomplete\nexport type WithNegativeSpacing<T extends string | number | symbol> =\n T extends string ? T | `-${T}` : T;\n\ntype ApplyDirectionProps = {\n currentValueOfCssVar: string | undefined;\n value: string;\n direction?: Direction;\n};\n\n// Helper function to parse directional values (handles calc() expressions)\nconst parseDirectionalValues = (\n value: string,\n): [string, string, string, string] => {\n const matches: string[] = [];\n let current = \"\";\n let depth = 0;\n\n for (let i = 0; i < value.length; i++) {\n const char = value[i];\n\n if (char === \"(\") {\n depth++;\n current += char;\n } else if (char === \")\") {\n depth--;\n current += char;\n } else if (char === \" \" && depth === 0) {\n // Space at depth 0 means we're between values\n if (current) {\n matches.push(current);\n current = \"\";\n }\n } else {\n current += char;\n }\n }\n\n // Push the last value\n if (current) {\n matches.push(current);\n }\n\n // Ensure we always have 4 values\n while (matches.length < 4) {\n matches.push(\"0\");\n }\n\n return [\n matches[0] || \"0\",\n matches[1] || \"0\",\n matches[2] || \"0\",\n matches[3] || \"0\",\n ];\n};\n\n// For values like margin and padding that require 4 values\nconst applyDirectionalValues = ({\n currentValueOfCssVar = \"0 0 0 0\",\n value,\n direction,\n}: ApplyDirectionProps) => {\n const [top, right, bottom, left] =\n parseDirectionalValues(currentValueOfCssVar);\n\n const newValues = {\n top,\n right,\n bottom,\n left,\n };\n\n if (direction === \"top\") {\n newValues.top = value;\n }\n\n if (direction === \"right\") {\n newValues.right = value;\n }\n\n if (direction === \"bottom\") {\n newValues.bottom = value;\n }\n\n if (direction === \"left\") {\n newValues.left = value;\n }\n\n if (direction === \"all\") {\n newValues.top = value;\n newValues.right = value;\n newValues.bottom = value;\n newValues.left = value;\n }\n\n if (direction === \"x\") {\n newValues.left = value;\n newValues.right = value;\n }\n\n if (direction === \"y\") {\n newValues.top = value;\n newValues.bottom = value;\n }\n\n // \"side\" direction is used for things like border-radius\n // where we are settings values on corners instead of sides\n // entire sides like padding and margin provide.\n if (direction === \"side-top\") {\n newValues.top = value;\n newValues.right = value;\n }\n\n if (direction === \"side-bottom\") {\n newValues.bottom = value;\n newValues.left = value;\n }\n\n if (direction === \"side-left\") {\n newValues.top = value;\n newValues.left = value;\n }\n\n if (direction === \"side-right\") {\n newValues.right = value;\n newValues.bottom = value;\n }\n\n const newValuesString = Object.values(newValues).join(\" \");\n\n return newValuesString;\n};\n\ntype ApplyAxisValuesProps = {\n currentValueOfCssVar: string | undefined;\n value: string;\n axis: Axis;\n};\n\nconst applyAxisValues = ({\n currentValueOfCssVar = \"visible visible\",\n value,\n axis,\n}: ApplyAxisValuesProps) => {\n const [x, y] = currentValueOfCssVar.split(\" \");\n\n // If the axis is x, we need to set the x value and keep the y value.\n if (axis === \"x\") {\n return `${value} ${y}`;\n }\n\n // If the axis is y, we need to set the y value and keep the x value.\n if (axis === \"y\") {\n return `${x} ${value}`;\n }\n\n // If the axis is both, we need to set the x and y values.\n return `${value} ${value}`;\n};\n\nexport type CssVarsPropObject<CssVars extends CssVarsPropObject<CssVars>> =\n Record<keyof CssVars, CssVarProp>;\n\ntype CssVarPropKey<CssVars extends CssVarsPropObject<CssVars>> = keyof CssVars;\n\n// Allow for explicitly defined props that are not css vars to end to end typesafe\ntype OtherProps<CssVars extends CssVarsPropObject<CssVars>, Props> =\n | Omit<\n {\n [key in keyof Props]: Props[key];\n },\n CssVarPropKey<CssVars>\n >\n | object;\n\n// Allow for explicitly defined css vars return css variables object created\n// by this function and be end to end typesafe\ntype StyleProp<CssVars extends CssVarsPropObject<CssVars>> =\n | {\n [key in CssVars[keyof CssVars][\"cssVar\"]]: string;\n }\n | object;\n\ntype GetStylePropParams<CssVars, Props> = {\n props: Props & { style?: Record<string, string> };\n cssVars: CssVars;\n};\n\n// Resolves a single prop value against its matching CssVarProp definition.\n// Returns the mapped CSS variable value string, handling negative spacing.\nconst resolveValue = (\n matchingCssVar: CssVarProp,\n propValue: string,\n): string => {\n const isNegative = typeof propValue === \"string\" && propValue.startsWith(\"-\");\n\n if (isNegative) {\n const positiveValue = propValue.slice(1);\n const positiveVar = matchingCssVar.value.replace(\"VARIABLE\", positiveValue);\n return `calc(-1 * ${positiveVar})`;\n }\n\n return matchingCssVar.value.replace(\"VARIABLE\", propValue);\n};\n\n// Applies a resolved CSS variable value to the styleProp object,\n// handling directional and axis properties.\nconst applyCssVar = <CssVars extends CssVarsPropObject<CssVars>>(\n styleProp: StyleProp<CssVars>,\n matchingCssVar: CssVarProp,\n mappedValue: string,\n cssVarNameOverride?: string,\n): StyleProp<CssVars> => {\n const cssVarName = (cssVarNameOverride ??\n matchingCssVar.cssVar) as keyof StyleProp<CssVars>;\n\n if (matchingCssVar.direction) {\n const currentValueOfCssVar = styleProp?.[cssVarName];\n const directionalValue = applyDirectionalValues({\n currentValueOfCssVar,\n value: mappedValue,\n direction: matchingCssVar.direction,\n });\n return { ...styleProp, [cssVarName]: directionalValue };\n }\n\n if (matchingCssVar.axis) {\n const currentValueOfCssVar = styleProp?.[cssVarName];\n const axisValue = applyAxisValues({\n currentValueOfCssVar,\n value: mappedValue,\n axis: matchingCssVar.axis,\n });\n return { ...styleProp, [cssVarName]: axisValue };\n }\n\n return { ...styleProp, [cssVarName]: mappedValue };\n};\n\n// Creates a state-prefixed CSS variable name from a base CSS variable.\n// e.g. (\"--background-color\", \"hover\") => \"--hover--background-color\"\nconst createPseudoCssVarName = (\n baseCssVar: string,\n pseudoState: PseudoState,\n): string => {\n const prefix = PSEUDO_CSS_PREFIX[pseudoState];\n const baseName = baseCssVar.replace(/^--/, \"\");\n return `--${prefix}--${baseName}`;\n};\n\nexport const getStyleProp = <\n CssVars extends CssVarsPropObject<CssVars>,\n Props extends Record<string, unknown>,\n>(\n params: GetStylePropParams<CssVars, Props>,\n): {\n styleProp: StyleProp<CssVars>;\n otherProps: OtherProps<CssVars, Props>;\n interactive: boolean;\n} => {\n const { cssVars } = params;\n\n if (!params?.props || Object.keys(params.props).length === 0) {\n return { styleProp: {}, otherProps: {}, interactive: false };\n }\n\n // Assign the additional styles to the style object so that it can be passed\n // to the component as a prop.\n const { style = {}, ...props } = params.props;\n\n let styleProp: StyleProp<CssVars> = style;\n const otherProps: OtherProps<CssVars, Props> = {};\n let interactive = false;\n\n Object.keys(props).forEach((_key) => {\n const key = _key as keyof typeof props;\n\n // Check if this is a pseudo-class object prop (hover, focus, active, etc.)\n if (\n PSEUDO_STATES.includes(_key as PseudoState) &&\n typeof props[key] === \"object\" &&\n props[key] !== null\n ) {\n const pseudoState = _key as PseudoState;\n const pseudoProps = props[key] as Record<string, string | undefined>;\n const unmatchedPseudoProps: Record<string, string> = {};\n\n Object.keys(pseudoProps).forEach((pseudoPropKey) => {\n const propValue = pseudoProps[pseudoPropKey];\n if (!propValue) return;\n\n const cssVarsKey = pseudoPropKey as unknown as keyof typeof cssVars;\n const matchingCssVar = cssVars?.[cssVarsKey];\n\n if (!matchingCssVar) {\n // Collect unmatched pseudo props to pass through to otherProps\n unmatchedPseudoProps[pseudoPropKey] = propValue;\n return;\n }\n\n const mappedValue = resolveValue(matchingCssVar, propValue);\n const pseudoCssVarName = createPseudoCssVarName(\n matchingCssVar.cssVar,\n pseudoState,\n );\n\n styleProp = applyCssVar(\n styleProp,\n matchingCssVar,\n mappedValue,\n pseudoCssVarName,\n );\n });\n\n // Pass through any unmatched pseudo props so downstream components\n // can process them (e.g. Button passes hover.backgroundColor to Box)\n if (Object.keys(unmatchedPseudoProps).length > 0) {\n const existingPseudo =\n (otherProps as Record<string, unknown>)[_key] || {};\n Object.assign(otherProps, {\n [_key]: { ...existingPseudo, ...unmatchedPseudoProps },\n });\n }\n\n // If any pseudo sub-props were matched against cssVars at this level,\n // mark as interactive so the component adds the scoping class.\n // This prevents pseudo-class CSS rules from cascading into child elements.\n if (\n Object.keys(pseudoProps).length >\n Object.keys(unmatchedPseudoProps).length\n ) {\n interactive = true;\n }\n\n return;\n }\n\n const cssVarsKey = key as unknown as keyof typeof cssVars;\n const matchingCssVar = cssVars?.[cssVarsKey];\n\n // If the prop is not a css var, just add it to the otherProps\n if (!matchingCssVar) {\n Object.assign(otherProps, { [key]: props[key] });\n return;\n }\n\n const matchingPropValue = props?.[key] as string | undefined;\n\n if (!matchingPropValue) {\n Object.assign(styleProp, { [key]: props[key] });\n return;\n }\n\n const mappedValueOfCssVar = resolveValue(matchingCssVar, matchingPropValue);\n\n styleProp = applyCssVar(styleProp, matchingCssVar, mappedValueOfCssVar);\n });\n\n return { styleProp, otherProps, interactive };\n};\n","import React from \"react\";\n\nimport { type CssVarProp, getStyleProp } from \"../helpers/getStyleProp\";\n\ntype CssVarsPropObject<CssVars> = Record<keyof CssVars, CssVarProp>;\n\nexport const useStyleEngine = <\n CssVars extends CssVarsPropObject<CssVars>,\n Props extends Record<string, unknown>,\n>(\n params: Parameters<typeof getStyleProp<CssVars, Props>>[0],\n) => {\n return React.useMemo(() => getStyleProp(params), [params]);\n};\n"],"names":["PSEUDO_STATES","PSEUDO_CSS_PREFIX","parseDirectionalValues","value","matches","current","depth","i","char","applyDirectionalValues","currentValueOfCssVar","direction","top","right","bottom","left","newValues","applyAxisValues","axis","x","y","resolveValue","matchingCssVar","propValue","positiveValue","applyCssVar","styleProp","mappedValue","cssVarNameOverride","cssVarName","directionalValue","axisValue","createPseudoCssVarName","baseCssVar","pseudoState","prefix","baseName","getStyleProp","params","cssVars","style","props","otherProps","interactive","_key","key","pseudoProps","unmatchedPseudoProps","pseudoPropKey","cssVarsKey","pseudoCssVarName","existingPseudo","matchingPropValue","mappedValueOfCssVar","useStyleEngine","React"],"mappings":";;;;;;;;;;;;;;GAyBaA,IAAgB;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAKMC,IAAiD;AAAA,EACrD,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,cAAc;AAAA,EACd,WAAW;AACb,GA0CMC,IAAyB,CAC7BC,MACqC;AACrC,QAAMC,IAAoB,CAAA;AAC1B,MAAIC,IAAU,IACVC,IAAQ;AAEZ,WAASC,IAAI,GAAGA,IAAIJ,EAAM,QAAQI,KAAK;AACrC,UAAMC,IAAOL,EAAMI,CAAC;AAEpB,IAAIC,MAAS,OACXF,KACAD,KAAWG,KACFA,MAAS,OAClBF,KACAD,KAAWG,KACFA,MAAS,OAAOF,MAAU,IAE/BD,MACFD,EAAQ,KAAKC,CAAO,GACpBA,IAAU,MAGZA,KAAWG;AAAA,EAEf;AAQA,OALIH,KACFD,EAAQ,KAAKC,CAAO,GAIfD,EAAQ,SAAS;AACtB,IAAAA,EAAQ,KAAK,GAAG;AAGlB,SAAO;AAAA,IACLA,EAAQ,CAAC,KAAK;AAAA,IACdA,EAAQ,CAAC,KAAK;AAAA,IACdA,EAAQ,CAAC,KAAK;AAAA,IACdA,EAAQ,CAAC,KAAK;AAAA,EAAA;AAElB,GAGMK,IAAyB,CAAC;AAAA,EAC9B,sBAAAC,IAAuB;AAAA,EACvB,OAAAP;AAAA,EACA,WAAAQ;AACF,MAA2B;AACzB,QAAM,CAACC,GAAKC,GAAOC,GAAQC,CAAI,IAC7Bb,EAAuBQ,CAAoB,GAEvCM,IAAY;AAAA,IAChB,KAAAJ;AAAA,IACA,OAAAC;AAAA,IACA,QAAAC;AAAA,IACA,MAAAC;AAAA,EAAA;AAGF,S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sBhC,EAAaC,GAAgB8B,CAAiB;AAE1E,IAAA1B,IAAYD,EAAYC,GAAWJ,GAAgB+B,CAAmB;AAAA,EACxE,CAAC,GAEM,EAAE,WAAA3B,GAAW,YAAAgB,GAAY,aAAAC,EAAA;AAClC,GC1aaW,IAAiB,CAI5BhB,MAEOiB,EAAM,QAAQ,MAAMlB,EAAaC,CAAM,GAAG,CAACA,CAAM,CAAC;"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../../tokens/dist/json/tokens.json","../../src/helpers/getStyleProp/getStyleProp.ts","../../src/hooks/useStyleEngine.ts"],"sourcesContent":["{\"border-style\":{\"solid\":\"var(--tgph-border-style-solid)\",\"dashed\":\"var(--tgph-border-style-dashed)\"},\"color\":{\"transparent\":\"var(--tgph-transparent)\",\"white\":\"var(--tgph-white)\",\"black\":\"var(--tgph-black)\",\"surface-1\":\"var(--tgph-surface-1)\",\"surface-2\":\"var(--tgph-surface-2)\",\"surface-3\":\"var(--tgph-surface-3)\",\"alpha-white-1\":\"var(--tgph-alpha-white-1)\",\"alpha-white-2\":\"var(--tgph-alpha-white-2)\",\"alpha-white-3\":\"var(--tgph-alpha-white-3)\",\"alpha-white-4\":\"var(--tgph-alpha-white-4)\",\"alpha-white-5\":\"var(--tgph-alpha-white-5)\",\"alpha-white-6\":\"var(--tgph-alpha-white-6)\",\"alpha-white-7\":\"var(--tgph-alpha-white-7)\",\"alpha-white-8\":\"var(--tgph-alpha-white-8)\",\"alpha-white-9\":\"var(--tgph-alpha-white-9)\",\"alpha-white-10\":\"var(--tgph-alpha-white-10)\",\"alpha-white-11\":\"var(--tgph-alpha-white-11)\",\"alpha-white-12\":\"var(--tgph-alpha-white-12)\",\"alpha-black-1\":\"var(--tgph-alpha-black-1)\",\"alpha-black-2\":\"var(--tgph-alpha-black-2)\",\"alpha-black-3\":\"var(--tgph-alpha-black-3)\",\"alpha-black-4\":\"var(--tgph-alpha-black-4)\",\"alpha-black-5\":\"var(--tgph-alpha-black-5)\",\"alpha-black-6\":\"var(--tgph-alpha-black-6)\",\"alpha-black-7\":\"var(--tgph-alpha-black-7)\",\"alpha-black-8\":\"var(--tgph-alpha-black-8)\",\"alpha-black-9\":\"var(--tgph-alpha-black-9)\",\"alpha-black-10\":\"var(--tgph-alpha-black-10)\",\"alpha-black-11\":\"var(--tgph-alpha-black-11)\",\"alpha-black-12\":\"var(--tgph-alpha-black-12)\",\"gray-1\":\"var(--tgph-gray-1)\",\"gray-2\":\"var(--tgph-gray-2)\",\"gray-3\":\"var(--tgph-gray-3)\",\"gray-4\":\"var(--tgph-gray-4)\",\"gray-5\":\"var(--tgph-gray-5)\",\"gray-6\":\"var(--tgph-gray-6)\",\"gray-7\":\"var(--tgph-gray-7)\",\"gray-8\":\"var(--tgph-gray-8)\",\"gray-9\":\"var(--tgph-gray-9)\",\"gray-10\":\"var(--tgph-gray-10)\",\"gray-11\":\"var(--tgph-gray-11)\",\"gray-12\":\"var(--tgph-gray-12)\",\"beige-1\":\"var(--tgph-beige-1)\",\"beige-2\":\"var(--tgph-beige-2)\",\"beige-3\":\"var(--tgph-beige-3)\",\"beige-4\":\"var(--tgph-beige-4)\",\"beige-5\":\"var(--tgph-beige-5)\",\"beige-6\":\"var(--tgph-beige-6)\",\"beige-7\":\"var(--tgph-beige-7)\",\"beige-8\":\"var(--tgph-beige-8)\",\"beige-9\":\"var(--tgph-beige-9)\",\"beige-10\":\"var(--tgph-beige-10)\",\"beige-11\":\"var(--tgph-beige-11)\",\"beige-12\":\"var(--tgph-beige-12)\",\"accent-1\":\"var(--tgph-accent-1)\",\"accent-2\":\"var(--tgph-accent-2)\",\"accent-3\":\"var(--tgph-accent-3)\",\"accent-4\":\"var(--tgph-accent-4)\",\"accent-5\":\"var(--tgph-accent-5)\",\"accent-6\":\"var(--tgph-accent-6)\",\"accent-7\":\"var(--tgph-accent-7)\",\"accent-8\":\"var(--tgph-accent-8)\",\"accent-9\":\"var(--tgph-accent-9)\",\"accent-10\":\"var(--tgph-accent-10)\",\"accent-11\":\"var(--tgph-accent-11)\",\"accent-12\":\"var(--tgph-accent-12)\",\"green-1\":\"var(--tgph-green-1)\",\"green-2\":\"var(--tgph-green-2)\",\"green-3\":\"var(--tgph-green-3)\",\"green-4\":\"var(--tgph-green-4)\",\"green-5\":\"var(--tgph-green-5)\",\"green-6\":\"var(--tgph-green-6)\",\"green-7\":\"var(--tgph-green-7)\",\"green-8\":\"var(--tgph-green-8)\",\"green-9\":\"var(--tgph-green-9)\",\"green-10\":\"var(--tgph-green-10)\",\"green-11\":\"var(--tgph-green-11)\",\"green-12\":\"var(--tgph-green-12)\",\"yellow-1\":\"var(--tgph-yellow-1)\",\"yellow-2\":\"var(--tgph-yellow-2)\",\"yellow-3\":\"var(--tgph-yellow-3)\",\"yellow-4\":\"var(--tgph-yellow-4)\",\"yellow-5\":\"var(--tgph-yellow-5)\",\"yellow-6\":\"var(--tgph-yellow-6)\",\"yellow-7\":\"var(--tgph-yellow-7)\",\"yellow-8\":\"var(--tgph-yellow-8)\",\"yellow-9\":\"var(--tgph-yellow-9)\",\"yellow-10\":\"var(--tgph-yellow-10)\",\"yellow-11\":\"var(--tgph-yellow-11)\",\"yellow-12\":\"var(--tgph-yellow-12)\",\"blue-1\":\"var(--tgph-blue-1)\",\"blue-2\":\"var(--tgph-blue-2)\",\"blue-3\":\"var(--tgph-blue-3)\",\"blue-4\":\"var(--tgph-blue-4)\",\"blue-5\":\"var(--tgph-blue-5)\",\"blue-6\":\"var(--tgph-blue-6)\",\"blue-7\":\"var(--tgph-blue-7)\",\"blue-8\":\"var(--tgph-blue-8)\",\"blue-9\":\"var(--tgph-blue-9)\",\"blue-10\":\"var(--tgph-blue-10)\",\"blue-11\":\"var(--tgph-blue-11)\",\"blue-12\":\"var(--tgph-blue-12)\",\"red-1\":\"var(--tgph-red-1)\",\"red-2\":\"var(--tgph-red-2)\",\"red-3\":\"var(--tgph-red-3)\",\"red-4\":\"var(--tgph-red-4)\",\"red-5\":\"var(--tgph-red-5)\",\"red-6\":\"var(--tgph-red-6)\",\"red-7\":\"var(--tgph-red-7)\",\"red-8\":\"var(--tgph-red-8)\",\"red-9\":\"var(--tgph-red-9)\",\"red-10\":\"var(--tgph-red-10)\",\"red-11\":\"var(--tgph-red-11)\",\"red-12\":\"var(--tgph-red-12)\",\"purple-1\":\"var(--tgph-purple-1)\",\"purple-2\":\"var(--tgph-purple-2)\",\"purple-3\":\"var(--tgph-purple-3)\",\"purple-4\":\"var(--tgph-purple-4)\",\"purple-5\":\"var(--tgph-purple-5)\",\"purple-6\":\"var(--tgph-purple-6)\",\"purple-7\":\"var(--tgph-purple-7)\",\"purple-8\":\"var(--tgph-purple-8)\",\"purple-9\":\"var(--tgph-purple-9)\",\"purple-10\":\"var(--tgph-purple-10)\",\"purple-11\":\"var(--tgph-purple-11)\",\"purple-12\":\"var(--tgph-purple-12)\"},\"rounded\":{\"0\":\"var(--tgph-rounded-0)\",\"1\":\"var(--tgph-rounded-1)\",\"2\":\"var(--tgph-rounded-2)\",\"3\":\"var(--tgph-rounded-3)\",\"4\":\"var(--tgph-rounded-4)\",\"5\":\"var(--tgph-rounded-5)\",\"6\":\"var(--tgph-rounded-6)\",\"full\":\"var(--tgph-rounded-full)\"},\"shadow\":{\"0\":\"var(--tgph-shadow-0)\",\"1\":\"var(--tgph-shadow-1)\",\"2\":\"var(--tgph-shadow-2)\",\"3\":\"var(--tgph-shadow-3)\",\"inner\":\"var(--tgph-shadow-inner)\"},\"spacing\":{\"0\":\"var(--tgph-spacing-0)\",\"1\":\"var(--tgph-spacing-1)\",\"2\":\"var(--tgph-spacing-2)\",\"3\":\"var(--tgph-spacing-3)\",\"4\":\"var(--tgph-spacing-4)\",\"5\":\"var(--tgph-spacing-5)\",\"6\":\"var(--tgph-spacing-6)\",\"7\":\"var(--tgph-spacing-7)\",\"8\":\"var(--tgph-spacing-8)\",\"9\":\"var(--tgph-spacing-9)\",\"10\":\"var(--tgph-spacing-10)\",\"11\":\"var(--tgph-spacing-11)\",\"12\":\"var(--tgph-spacing-12)\",\"14\":\"var(--tgph-spacing-14)\",\"16\":\"var(--tgph-spacing-16)\",\"20\":\"var(--tgph-spacing-20)\",\"24\":\"var(--tgph-spacing-24)\",\"28\":\"var(--tgph-spacing-28)\",\"32\":\"var(--tgph-spacing-32)\",\"36\":\"var(--tgph-spacing-36)\",\"40\":\"var(--tgph-spacing-40)\",\"44\":\"var(--tgph-spacing-44)\",\"48\":\"var(--tgph-spacing-48)\",\"52\":\"var(--tgph-spacing-52)\",\"56\":\"var(--tgph-spacing-56)\",\"60\":\"var(--tgph-spacing-60)\",\"64\":\"var(--tgph-spacing-64)\",\"72\":\"var(--tgph-spacing-72)\",\"80\":\"var(--tgph-spacing-80)\",\"96\":\"var(--tgph-spacing-96)\",\"120\":\"var(--tgph-spacing-120)\",\"140\":\"var(--tgph-spacing-140)\",\"160\":\"var(--tgph-spacing-160)\",\"px\":\"var(--tgph-spacing-px)\",\"0_5\":\"var(--tgph-spacing-0_5)\",\"1_5\":\"var(--tgph-spacing-1_5)\",\"2_5\":\"var(--tgph-spacing-2_5)\",\"3_5\":\"var(--tgph-spacing-3_5)\",\"full\":\"var(--tgph-spacing-full)\",\"auto\":\"var(--tgph-spacing-auto)\"},\"family\":{\"sans\":\"var(--tgph-family-sans)\",\"mono\":\"var(--tgph-family-mono)\"},\"leading\":{\"0\":\"var(--tgph-leading-0)\",\"1\":\"var(--tgph-leading-1)\",\"2\":\"var(--tgph-leading-2)\",\"3\":\"var(--tgph-leading-3)\",\"4\":\"var(--tgph-leading-4)\",\"5\":\"var(--tgph-leading-5)\",\"6\":\"var(--tgph-leading-6)\",\"7\":\"var(--tgph-leading-7)\",\"8\":\"var(--tgph-leading-8)\",\"9\":\"var(--tgph-leading-9)\",\"code-0\":\"var(--tgph-leading-code-0)\",\"code-1\":\"var(--tgph-leading-code-1)\",\"code-2\":\"var(--tgph-leading-code-2)\",\"code-3\":\"var(--tgph-leading-code-3)\",\"code-4\":\"var(--tgph-leading-code-4)\",\"code-5\":\"var(--tgph-leading-code-5)\",\"code-6\":\"var(--tgph-leading-code-6)\",\"code-7\":\"var(--tgph-leading-code-7)\",\"code-8\":\"var(--tgph-leading-code-8)\",\"code-9\":\"var(--tgph-leading-code-9)\"},\"tracking\":{\"0\":\"var(--tgph-tracking-0)\",\"1\":\"var(--tgph-tracking-1)\",\"2\":\"var(--tgph-tracking-2)\",\"3\":\"var(--tgph-tracking-3)\",\"4\":\"var(--tgph-tracking-4)\",\"5\":\"var(--tgph-tracking-5)\",\"6\":\"var(--tgph-tracking-6)\",\"7\":\"var(--tgph-tracking-7)\",\"8\":\"var(--tgph-tracking-8)\",\"9\":\"var(--tgph-tracking-9)\"},\"text\":{\"0\":\"var(--tgph-text-0)\",\"1\":\"var(--tgph-text-1)\",\"2\":\"var(--tgph-text-2)\",\"3\":\"var(--tgph-text-3)\",\"4\":\"var(--tgph-text-4)\",\"5\":\"var(--tgph-text-5)\",\"6\":\"var(--tgph-text-6)\",\"7\":\"var(--tgph-text-7)\",\"8\":\"var(--tgph-text-8)\",\"9\":\"var(--tgph-text-9)\",\"code-0\":\"var(--tgph-text-code-0)\",\"code-1\":\"var(--tgph-text-code-1)\",\"code-2\":\"var(--tgph-text-code-2)\",\"code-4\":\"var(--tgph-text-code-4)\",\"code-5\":\"var(--tgph-text-code-5)\",\"code-6\":\"var(--tgph-text-code-6)\",\"code-7\":\"var(--tgph-text-code-7)\",\"code-8\":\"var(--tgph-text-code-8)\",\"code-9\":\"var(--tgph-text-code-9)\"},\"weight\":{\"regular\":\"var(--tgph-weight-regular)\",\"medium\":\"var(--tgph-weight-medium)\",\"semi-bold\":\"var(--tgph-weight-semi-bold)\"},\"breakpoint\":{\"sm\":\"var(--tgph-breakpoint-sm)\",\"md\":\"var(--tgph-breakpoint-md)\",\"lg\":\"var(--tgph-breakpoint-lg)\",\"xl\":\"var(--tgph-breakpoint-xl)\",\"2xl\":\"var(--tgph-breakpoint-2xl)\"},\"zIndex\":{\"hidden\":\"var(--tgph-zIndex-hidden)\",\"base\":\"var(--tgph-zIndex-base)\",\"auto\":\"var(--tgph-zIndex-auto)\",\"dropdown\":\"var(--tgph-zIndex-dropdown)\",\"sticky\":\"var(--tgph-zIndex-sticky)\",\"banner\":\"var(--tgph-zIndex-banner)\",\"overlay\":\"var(--tgph-zIndex-overlay)\",\"modal\":\"var(--tgph-zIndex-modal)\",\"popover\":\"var(--tgph-zIndex-popover)\",\"skipLink\":\"var(--tgph-zIndex-skipLink)\",\"toast\":\"var(--tgph-zIndex-toast)\",\"tooltip\":\"var(--tgph-zIndex-tooltip)\"}}","type Direction =\n | \"x\"\n | \"y\"\n | \"top\"\n | \"bottom\"\n | \"left\"\n | \"right\"\n | \"all\"\n | \"side-top\"\n | \"side-bottom\"\n | \"side-left\"\n | \"side-right\";\n\ntype Axis = \"x\" | \"y\" | \"both\";\n\nexport type CssVarProp = {\n cssVar: string;\n value: string;\n direction?: Direction;\n axis?: Axis;\n};\n\n// Supported pseudo-class states for the object syntax\n// Prefixed with underscore to avoid collisions with native HTML attributes\n// (e.g. `disabled`) and common component props (e.g. `active`).\nexport const PSEUDO_STATES = [\n \"_hover\",\n \"_focus\",\n \"_active\",\n \"_focusWithin\",\n \"_disabled\",\n] as const;\n\nexport type PseudoState = (typeof PSEUDO_STATES)[number];\n\n// Maps pseudo state names to CSS variable prefixes\nconst PSEUDO_CSS_PREFIX: Record<PseudoState, string> = {\n _hover: \"hover\",\n _focus: \"focus\",\n _active: \"active\",\n _focusWithin: \"focus-within\",\n _disabled: \"disabled\",\n};\n\n/**\n * Adds pseudo-class variant props to a style props type.\n *\n * Given a base style props type, this creates a new type that also\n * accepts `_hover`, `_focus`, `_active`, `_focusWithin`, and `_disabled`\n * props as objects containing any of the base style props.\n *\n * Prefixed with underscore to avoid collisions with native HTML attributes\n * (e.g. `disabled`) and common component props (e.g. `active`).\n *\n * @example\n * type BoxStyle = { bg: ColorToken; p: SpacingToken };\n * type BoxStyleWithPseudo = WithPseudo<BoxStyle>;\n * // Allows: { bg: \"gray-2\", _hover: { bg: \"gray-3\" }, _focus: { p: \"4\" } }\n */\nexport type WithPseudo<Props> = Props & {\n _hover?: Partial<Props>;\n _focus?: Partial<Props>;\n _active?: Partial<Props>;\n _focusWithin?: Partial<Props>;\n _disabled?: Partial<Props>;\n};\n\n// Helper type to create negative spacing values\n// This distributes over union types for proper type expansion\nexport type NegativeSpacing<T extends string | number | symbol> =\n T extends string ? `-${T}` : never;\n\n// Type that allows both positive and negative spacing values for a given token type\n// Using direct template literal for better TypeScript inference and autocomplete\nexport type WithNegativeSpacing<T extends string | number | symbol> =\n T extends string ? T | `-${T}` : T;\n\ntype ApplyDirectionProps = {\n currentValueOfCssVar: string | undefined;\n value: string;\n direction?: Direction;\n};\n\n// Helper function to parse directional values (handles calc() expressions)\nconst parseDirectionalValues = (\n value: string,\n): [string, string, string, string] => {\n const matches: string[] = [];\n let current = \"\";\n let depth = 0;\n\n for (let i = 0; i < value.length; i++) {\n const char = value[i];\n\n if (char === \"(\") {\n depth++;\n current += char;\n } else if (char === \")\") {\n depth--;\n current += char;\n } else if (char === \" \" && depth === 0) {\n // Space at depth 0 means we're between values\n if (current) {\n matches.push(current);\n current = \"\";\n }\n } else {\n current += char;\n }\n }\n\n // Push the last value\n if (current) {\n matches.push(current);\n }\n\n // Ensure we always have 4 values\n while (matches.length < 4) {\n matches.push(\"0\");\n }\n\n return [\n matches[0] || \"0\",\n matches[1] || \"0\",\n matches[2] || \"0\",\n matches[3] || \"0\",\n ];\n};\n\n// For values like margin and padding that require 4 values\nconst applyDirectionalValues = ({\n currentValueOfCssVar = \"0 0 0 0\",\n value,\n direction,\n}: ApplyDirectionProps) => {\n const [top, right, bottom, left] =\n parseDirectionalValues(currentValueOfCssVar);\n\n const newValues = {\n top,\n right,\n bottom,\n left,\n };\n\n if (direction === \"top\") {\n newValues.top = value;\n }\n\n if (direction === \"right\") {\n newValues.right = value;\n }\n\n if (direction === \"bottom\") {\n newValues.bottom = value;\n }\n\n if (direction === \"left\") {\n newValues.left = value;\n }\n\n if (direction === \"all\") {\n newValues.top = value;\n newValues.right = value;\n newValues.bottom = value;\n newValues.left = value;\n }\n\n if (direction === \"x\") {\n newValues.left = value;\n newValues.right = value;\n }\n\n if (direction === \"y\") {\n newValues.top = value;\n newValues.bottom = value;\n }\n\n // \"side\" direction is used for things like border-radius\n // where we are settings values on corners instead of sides\n // entire sides like padding and margin provide.\n if (direction === \"side-top\") {\n newValues.top = value;\n newValues.right = value;\n }\n\n if (direction === \"side-bottom\") {\n newValues.bottom = value;\n newValues.left = value;\n }\n\n if (direction === \"side-left\") {\n newValues.top = value;\n newValues.left = value;\n }\n\n if (direction === \"side-right\") {\n newValues.right = value;\n newValues.bottom = value;\n }\n\n const newValuesString = Object.values(newValues).join(\" \");\n\n return newValuesString;\n};\n\ntype ApplyAxisValuesProps = {\n currentValueOfCssVar: string | undefined;\n value: string;\n axis: Axis;\n};\n\nconst applyAxisValues = ({\n currentValueOfCssVar = \"visible visible\",\n value,\n axis,\n}: ApplyAxisValuesProps) => {\n const [x, y] = currentValueOfCssVar.split(\" \");\n\n // If the axis is x, we need to set the x value and keep the y value.\n if (axis === \"x\") {\n return `${value} ${y}`;\n }\n\n // If the axis is y, we need to set the y value and keep the x value.\n if (axis === \"y\") {\n return `${x} ${value}`;\n }\n\n // If the axis is both, we need to set the x and y values.\n return `${value} ${value}`;\n};\n\nexport type CssVarsPropObject<CssVars extends CssVarsPropObject<CssVars>> =\n Record<keyof CssVars, CssVarProp>;\n\ntype CssVarPropKey<CssVars extends CssVarsPropObject<CssVars>> = keyof CssVars;\n\n// Allow for explicitly defined props that are not css vars to end to end typesafe\ntype OtherProps<CssVars extends CssVarsPropObject<CssVars>, Props> =\n | Omit<\n {\n [key in keyof Props]: Props[key];\n },\n CssVarPropKey<CssVars>\n >\n | object;\n\n// Allow for explicitly defined css vars return css variables object created\n// by this function and be end to end typesafe\ntype StyleProp<CssVars extends CssVarsPropObject<CssVars>> =\n | {\n [key in CssVars[keyof CssVars][\"cssVar\"]]: string;\n }\n | object;\n\ntype GetStylePropParams<CssVars, Props> = {\n props: Props & { style?: Record<string, string> };\n cssVars: CssVars;\n};\n\n// Resolves a single prop value against its matching CssVarProp definition.\n// Returns the mapped CSS variable value string, handling negative spacing.\nconst resolveValue = (\n matchingCssVar: CssVarProp,\n propValue: string,\n): string => {\n const isNegative = typeof propValue === \"string\" && propValue.startsWith(\"-\");\n\n if (isNegative) {\n const positiveValue = propValue.slice(1);\n const positiveVar = matchingCssVar.value.replace(\"VARIABLE\", positiveValue);\n return `calc(-1 * ${positiveVar})`;\n }\n\n return matchingCssVar.value.replace(\"VARIABLE\", propValue);\n};\n\n// Applies a resolved CSS variable value to the styleProp object,\n// handling directional and axis properties.\nconst applyCssVar = <CssVars extends CssVarsPropObject<CssVars>>(\n styleProp: StyleProp<CssVars>,\n matchingCssVar: CssVarProp,\n mappedValue: string,\n cssVarNameOverride?: string,\n): StyleProp<CssVars> => {\n const cssVarName = (cssVarNameOverride ??\n matchingCssVar.cssVar) as keyof StyleProp<CssVars>;\n\n if (matchingCssVar.direction) {\n const currentValueOfCssVar = styleProp?.[cssVarName];\n const directionalValue = applyDirectionalValues({\n currentValueOfCssVar,\n value: mappedValue,\n direction: matchingCssVar.direction,\n });\n return { ...styleProp, [cssVarName]: directionalValue };\n }\n\n if (matchingCssVar.axis) {\n const currentValueOfCssVar = styleProp?.[cssVarName];\n const axisValue = applyAxisValues({\n currentValueOfCssVar,\n value: mappedValue,\n axis: matchingCssVar.axis,\n });\n return { ...styleProp, [cssVarName]: axisValue };\n }\n\n return { ...styleProp, [cssVarName]: mappedValue };\n};\n\n// Creates a state-prefixed CSS variable name from a base CSS variable.\n// e.g. (\"--background-color\", \"hover\") => \"--hover--background-color\"\nconst createPseudoCssVarName = (\n baseCssVar: string,\n pseudoState: PseudoState,\n): string => {\n const prefix = PSEUDO_CSS_PREFIX[pseudoState];\n const baseName = baseCssVar.replace(/^--/, \"\");\n return `--${prefix}--${baseName}`;\n};\n\nexport const getStyleProp = <\n CssVars extends CssVarsPropObject<CssVars>,\n Props extends Record<string, unknown>,\n>(\n params: GetStylePropParams<CssVars, Props>,\n): {\n styleProp: StyleProp<CssVars>;\n otherProps: OtherProps<CssVars, Props>;\n interactive: boolean;\n} => {\n const { cssVars } = params;\n\n if (!params?.props || Object.keys(params.props).length === 0) {\n return { styleProp: {}, otherProps: {}, interactive: false };\n }\n\n // Assign the additional styles to the style object so that it can be passed\n // to the component as a prop.\n const { style = {}, ...props } = params.props;\n\n let styleProp: StyleProp<CssVars> = style;\n const otherProps: OtherProps<CssVars, Props> = {};\n let interactive = false;\n\n Object.keys(props).forEach((_key) => {\n const key = _key as keyof typeof props;\n\n // Check if this is a pseudo-class object prop (hover, focus, active, etc.)\n if (\n PSEUDO_STATES.includes(_key as PseudoState) &&\n typeof props[key] === \"object\" &&\n props[key] !== null\n ) {\n const pseudoState = _key as PseudoState;\n const pseudoProps = props[key] as Record<string, string | undefined>;\n const unmatchedPseudoProps: Record<string, string> = {};\n\n Object.keys(pseudoProps).forEach((pseudoPropKey) => {\n const propValue = pseudoProps[pseudoPropKey];\n if (!propValue) return;\n\n const cssVarsKey = pseudoPropKey as unknown as keyof typeof cssVars;\n const matchingCssVar = cssVars?.[cssVarsKey];\n\n if (!matchingCssVar) {\n // Collect unmatched pseudo props to pass through to otherProps\n unmatchedPseudoProps[pseudoPropKey] = propValue;\n return;\n }\n\n const mappedValue = resolveValue(matchingCssVar, propValue);\n const pseudoCssVarName = createPseudoCssVarName(\n matchingCssVar.cssVar,\n pseudoState,\n );\n\n styleProp = applyCssVar(\n styleProp,\n matchingCssVar,\n mappedValue,\n pseudoCssVarName,\n );\n });\n\n // Pass through any unmatched pseudo props so downstream components\n // can process them (e.g. Button passes hover.backgroundColor to Box)\n if (Object.keys(unmatchedPseudoProps).length > 0) {\n const existingPseudo =\n (otherProps as Record<string, unknown>)[_key] || {};\n Object.assign(otherProps, {\n [_key]: { ...existingPseudo, ...unmatchedPseudoProps },\n });\n }\n\n // If any pseudo sub-props were matched against cssVars at this level,\n // mark as interactive so the component adds the scoping class.\n // This prevents pseudo-class CSS rules from cascading into child elements.\n if (\n Object.keys(pseudoProps).length >\n Object.keys(unmatchedPseudoProps).length\n ) {\n interactive = true;\n }\n\n return;\n }\n\n const cssVarsKey = key as unknown as keyof typeof cssVars;\n const matchingCssVar = cssVars?.[cssVarsKey];\n\n // If the prop is not a css var, just add it to the otherProps\n if (!matchingCssVar) {\n Object.assign(otherProps, { [key]: props[key] });\n return;\n }\n\n const matchingPropValue = props?.[key] as string | undefined;\n\n if (!matchingPropValue) {\n Object.assign(styleProp, { [key]: props[key] });\n return;\n }\n\n const mappedValueOfCssVar = resolveValue(matchingCssVar, matchingPropValue);\n\n styleProp = applyCssVar(styleProp, matchingCssVar, mappedValueOfCssVar);\n });\n\n return { styleProp, otherProps, interactive };\n};\n","import React from \"react\";\n\nimport { type CssVarProp, getStyleProp } from \"../helpers/getStyleProp\";\n\ntype CssVarsPropObject<CssVars> = Record<keyof CssVars, CssVarProp>;\n\nexport const useStyleEngine = <\n CssVars extends CssVarsPropObject<CssVars>,\n Props extends Record<string, unknown>,\n>(\n params: Parameters<typeof getStyleProp<CssVars, Props>>[0],\n) => {\n return React.useMemo(() => getStyleProp(params), [params]);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GCyBa,IAAgB;CAC3B;CACA;CACA;CACA;CACA;CACD,EAKK,IAAiD;CACrD,QAA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
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var e = (e, t) => () => (t || e((t = { exports: {} }).exports, t), t.exports), t = /* @__PURE__ */ ((e) => typeof require < "u" ? require : typeof Proxy < "u" ? new Proxy(e, { get: (e, t) => (typeof require < "u" ? require : e)[t] }) : e)(function(e) {
|
|
3
|
+
if (typeof require < "u") return require.apply(this, arguments);
|
|
4
|
+
throw Error("Calling `require` for \"" + e + "\" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.");
|
|
5
|
+
}), n = /* @__PURE__ */ e(((e, t) => {
|
|
6
|
+
t.exports = {};
|
|
7
|
+
})), r = n(), i = n(), a = t("postcss");
|
|
8
|
+
function o(e) {
|
|
9
|
+
let t = e;
|
|
10
|
+
for (;;) {
|
|
11
|
+
let e = r.join(t, "package.json");
|
|
12
|
+
if (i.existsSync(e)) return t;
|
|
13
|
+
let n = r.dirname(t);
|
|
14
|
+
if (n === t) return;
|
|
15
|
+
t = n;
|
|
16
|
+
}
|
|
12
17
|
}
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const c = i.dirname(s);
|
|
24
|
-
c === s && (e = !1), s = c;
|
|
25
|
-
}
|
|
18
|
+
function s(e = process.cwd()) {
|
|
19
|
+
let t = e, n = !0;
|
|
20
|
+
for (; n;) {
|
|
21
|
+
let e = r.join(t, "package.json");
|
|
22
|
+
if (i.existsSync(e)) try {
|
|
23
|
+
if (JSON.parse(i.readFileSync(e, "utf8")).workspaces) return t;
|
|
24
|
+
} catch {}
|
|
25
|
+
let a = r.dirname(t);
|
|
26
|
+
a === t && (n = !1), t = a;
|
|
27
|
+
}
|
|
26
28
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
path: i.resolve(s, "node_modules", c)
|
|
34
|
-
}), n), {});
|
|
29
|
+
var c = (e, t) => {
|
|
30
|
+
if (!(!e.dependencies || Object.keys(e.dependencies)?.length === 0)) return Object.entries(e.dependencies).reduce((e, [n, i]) => (n.startsWith("@telegraph/") && (e[n] = {
|
|
31
|
+
name: n,
|
|
32
|
+
version: i,
|
|
33
|
+
path: r.resolve(t, "node_modules", n)
|
|
34
|
+
}), e), {});
|
|
35
35
|
};
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
...a,
|
|
67
|
-
path: i.resolve(l, "node_modules", a.name)
|
|
68
|
-
}) : (d = h, f = e);
|
|
69
|
-
}
|
|
70
|
-
try {
|
|
71
|
-
const h = JSON.parse(p.readFileSync(d, "utf8")), g = m(h, f);
|
|
72
|
-
if (Object.assign(r, g), g) {
|
|
73
|
-
const y = k(g);
|
|
74
|
-
Object.assign(r, y);
|
|
75
|
-
}
|
|
76
|
-
} catch {
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return r;
|
|
81
|
-
};
|
|
82
|
-
return o ? k(o) : {};
|
|
36
|
+
function l(e) {
|
|
37
|
+
let t = o(e ? r.dirname(e) : process.cwd()) ?? process.cwd(), n = r.resolve(t, "package.json"), a = c(JSON.parse(i.readFileSync(n, "utf8")), t), l = s(t), u = (e) => {
|
|
38
|
+
if (!e || Object.keys(e).length === 0) return e;
|
|
39
|
+
let n = { ...e };
|
|
40
|
+
for (let a of Object.values(e)) {
|
|
41
|
+
let e, o;
|
|
42
|
+
if (a.version.includes("workspace:") && l) e = r.resolve(l, "node_modules", a.name, "package.json"), o = l, n[a.name] = {
|
|
43
|
+
...a,
|
|
44
|
+
path: r.resolve(l, "node_modules", a.name)
|
|
45
|
+
};
|
|
46
|
+
else {
|
|
47
|
+
let s = r.resolve(t, "node_modules", a.name, "package.json"), c = l && r.resolve(l, "node_modules", a.name, "package.json");
|
|
48
|
+
i.existsSync(s) ? (e = s, o = t) : c && i.existsSync(c) ? (e = c, o = l, n[a.name] = {
|
|
49
|
+
...a,
|
|
50
|
+
path: r.resolve(l, "node_modules", a.name)
|
|
51
|
+
}) : (e = s, o = t);
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
let t = c(JSON.parse(i.readFileSync(e, "utf8")), o);
|
|
55
|
+
if (Object.assign(n, t), t) {
|
|
56
|
+
let e = u(t);
|
|
57
|
+
Object.assign(n, e);
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return n;
|
|
64
|
+
};
|
|
65
|
+
return a ? u(a) : {};
|
|
83
66
|
}
|
|
84
|
-
function
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
} catch {
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
67
|
+
function u({ fileName: e = "default.css", path: t }) {
|
|
68
|
+
try {
|
|
69
|
+
let n = `${t}/dist/css/${e}`, a = r.resolve(n);
|
|
70
|
+
return i.existsSync(a) ? i.readFileSync(a, "utf8") : null;
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
94
74
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
config: {
|
|
132
|
-
tokens: e.tokens,
|
|
133
|
-
components: e.components
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
]
|
|
139
|
-
}), b = Object.assign(J, {
|
|
140
|
-
postcss: !0
|
|
141
|
-
});
|
|
142
|
-
export {
|
|
143
|
-
b as default
|
|
144
|
-
};
|
|
145
|
-
//# sourceMappingURL=postcss.mjs.map
|
|
75
|
+
var d = async ({ root: e, cssFilePath: t, config: n }) => {
|
|
76
|
+
let r = l(t), i = n.components === !0 ? Object.values(r).filter((e) => !e.name.includes("@telegraph/tokens")) : [], o = n.tokens.length > 0 ? Object.values(r).filter((e) => e.name.includes("@telegraph/tokens")) : [], s = i.map((e) => u({ path: e.path })).filter(Boolean), c = n.tokens.map((e) => o.map((t) => u({
|
|
77
|
+
path: t.path,
|
|
78
|
+
fileName: `${e}.css`
|
|
79
|
+
})).filter(Boolean)).filter(Boolean), d = [...s, ...c];
|
|
80
|
+
for (let t of d) {
|
|
81
|
+
let n = a.parse(t);
|
|
82
|
+
e.append(n);
|
|
83
|
+
}
|
|
84
|
+
}, f = Object.assign(() => ({
|
|
85
|
+
postcssPlugin: "@telegraph/style-engine",
|
|
86
|
+
plugins: [{
|
|
87
|
+
postcssPlugin: "telegraph",
|
|
88
|
+
AtRule: { telegraph() {} },
|
|
89
|
+
async Once(e) {
|
|
90
|
+
let t = e.source?.input?.file, n = {
|
|
91
|
+
tokens: [],
|
|
92
|
+
components: !1
|
|
93
|
+
};
|
|
94
|
+
e.walkAtRules("telegraph", (e) => {
|
|
95
|
+
e.params === "components" && (n.components = !0, e.remove()), e.params === "tokens-light" && (n.tokens.push("light"), e.remove()), e.params === "tokens-dark" && (n.tokens.push("dark"), e.remove()), e.params === "tokens" && (n.tokens.push("default"), e.remove());
|
|
96
|
+
}), await d({
|
|
97
|
+
root: e,
|
|
98
|
+
cssFilePath: t,
|
|
99
|
+
config: {
|
|
100
|
+
tokens: n.tokens,
|
|
101
|
+
components: n.components
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}]
|
|
106
|
+
}), { postcss: !0 });
|
|
107
|
+
//#endregion
|
|
108
|
+
export { f as default };
|
|
109
|
+
|
|
110
|
+
//# sourceMappingURL=postcss.mjs.map
|
package/dist/esm/postcss.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postcss.mjs","sources":["../../src/plugins/postcss.ts"],"sourcesContent":["import { type AcceptedPlugin, type PluginCreator, type Root } from \"postcss\";\n\n// Using require() instead of import to prevent ESM-related bugs in PostCSS.\n// ESM = ECMAScript Modules (the import/export syntax)\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodePath = require(\"node:path\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodeFs = require(\"node:fs\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst postcss = require(\"postcss\");\n\ntype PkgJson = {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n};\n\ntype DepObject = Record<\n string,\n {\n name: string;\n version: string;\n path: string;\n }\n>;\n\n/**\n * Traverses up the directory tree from a given start path to find the nearest package.json.\n * Returns the directory containing that package.json, or undefined if none is found.\n * Used to resolve the correct consumer package when process.cwd() may be the monorepo root.\n */\nfunction findNearestPackageJsonDir(start: string): string | undefined {\n let current = start;\n\n while (true) {\n const pkgJsonPath = nodePath.join(current, \"package.json\");\n if (nodeFs.existsSync(pkgJsonPath)) {\n return current;\n }\n\n const parent = nodePath.dirname(current);\n if (parent === current) return undefined;\n current = parent;\n }\n}\n\n/**\n * Traverses up the directory tree to find the root of the monorepo by looking for a package.json with workspaces.\n * This is needed to properly resolve workspace dependencies in a monorepo setup.\n */\nfunction findMonorepoRoot(start = process.cwd()): string | undefined {\n let current = start;\n let run = true;\n\n while (run) {\n const pkgJsonPath = nodePath.join(current, \"package.json\");\n if (nodeFs.existsSync(pkgJsonPath)) {\n try {\n const pkg = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n if (pkg.workspaces) {\n return current;\n }\n } catch {\n // malformed package.json, skip\n }\n }\n\n const parent = nodePath.dirname(current);\n if (parent === current) run = false;\n current = parent;\n }\n\n return undefined;\n}\n\n/**\n * Extracts all @telegraph/* dependencies from a package.json file and returns them with their paths.\n * This is used to find all Telegraph packages that might contain CSS we need to include.\n */\nconst getTelegraphDepsFromPackageJson = (\n pkg: PkgJson,\n pkgPath: string,\n): DepObject | undefined => {\n if (!pkg.dependencies || Object.keys(pkg.dependencies)?.length === 0) return;\n\n return Object.entries(pkg.dependencies).reduce((acc, [dep, version]) => {\n if (dep.startsWith(\"@telegraph/\")) {\n acc[dep] = {\n name: dep,\n version,\n path: nodePath.resolve(pkgPath, \"node_modules\", dep),\n };\n }\n return acc;\n }, {} as DepObject);\n};\n\n/**\n * Gets all Telegraph dependencies recursively, including dependencies of dependencies.\n * Handles both normal npm dependencies and workspace dependencies in a monorepo.\n *\n * @param cssFilePath - The absolute path of the CSS file being processed by PostCSS.\n * When provided, we walk up from the CSS file's directory to find the nearest\n * package.json. This ensures we read the *consumer's* package.json rather than\n * the monorepo root's package.json (which has no @telegraph/* deps), fixing the\n * Turborepo issue where process.cwd() is always the repo root during builds.\n */\nfunction getTelegraphDeps(cssFilePath?: string): DepObject {\n // Prefer to start from the CSS file's directory so we find the consumer's\n // package.json. Fall back to process.cwd() for in-memory PostCSS usage.\n const startDir = cssFilePath ? nodePath.dirname(cssFilePath) : process.cwd();\n const consumerDir = findNearestPackageJsonDir(startDir) ?? process.cwd();\n\n const pkgPath = nodePath.resolve(consumerDir, \"package.json\");\n const pkg = JSON.parse(nodeFs.readFileSync(pkgPath, \"utf8\"));\n const topLevelDeps = getTelegraphDepsFromPackageJson(pkg, consumerDir);\n const monorepoRoot = findMonorepoRoot(consumerDir);\n\n const recursivelyGetTelegraphDeps = (deps: DepObject): DepObject => {\n if (!deps || Object.keys(deps).length === 0) {\n return deps;\n }\n\n const allDeps = { ...deps };\n\n for (const dep of Object.values(deps)) {\n let pkgJsonPath: string;\n let searchPath: string;\n\n if (dep.version.includes(\"workspace:\") && monorepoRoot) {\n pkgJsonPath = nodePath.resolve(\n monorepoRoot,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n searchPath = monorepoRoot;\n // Workspace deps are hoisted to the monorepo root's node_modules.\n // The path stored in DepObject was set relative to consumerDir when the\n // dep was first recorded, so correct it to the monorepo root here so\n // that getCssStyles() can find the actual dist/css/default.css file.\n allDeps[dep.name] = {\n ...dep,\n path: nodePath.resolve(monorepoRoot, \"node_modules\", dep.name),\n };\n } else {\n // Non-workspace dep: try consumerDir/node_modules first, then fall\n // back to monorepoRoot/node_modules. Yarn (and other package managers)\n // hoist all deps to the root node_modules regardless of whether they\n // use workspace: protocol, so a pinned semver dep like \"0.1.1\" will\n // typically live at the monorepo root, not the consumer package dir.\n const localPkgJsonPath = nodePath.resolve(\n consumerDir,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n const rootPkgJsonPath =\n monorepoRoot &&\n nodePath.resolve(\n monorepoRoot,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n\n if (nodeFs.existsSync(localPkgJsonPath)) {\n pkgJsonPath = localPkgJsonPath;\n searchPath = consumerDir;\n } else if (rootPkgJsonPath && nodeFs.existsSync(rootPkgJsonPath)) {\n pkgJsonPath = rootPkgJsonPath;\n searchPath = monorepoRoot!;\n // Correct the stored path so getCssStyles() finds the right dist/css/ dir.\n allDeps[dep.name] = {\n ...dep,\n path: nodePath.resolve(monorepoRoot!, \"node_modules\", dep.name),\n };\n } else {\n pkgJsonPath = localPkgJsonPath; // will throw on readFileSync, caught below\n searchPath = consumerDir;\n }\n }\n\n try {\n const pkgJson = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n const deps = getTelegraphDepsFromPackageJson(pkgJson, searchPath);\n\n // Merge new dependencies into allDeps\n Object.assign(allDeps, deps);\n\n if (deps) {\n // Recursively get dependencies of dependencies\n const nestedDeps = recursivelyGetTelegraphDeps(deps);\n Object.assign(allDeps, nestedDeps);\n }\n } catch (_err) {\n // Skip if package.json cannot be read\n continue;\n }\n }\n\n return allDeps;\n };\n\n if (topLevelDeps) {\n return recursivelyGetTelegraphDeps(topLevelDeps);\n }\n\n return {};\n}\n\ntype GetCssStylesParams = {\n fileName?: string;\n path: string;\n};\n\n/**\n * Reads CSS file content from a Telegraph package's dist/css directory.\n * Returns null if the file doesn't exist or can't be read.\n */\nfunction getCssStyles({\n fileName = \"default.css\",\n path,\n}: GetCssStylesParams): string | null {\n try {\n const cssPath = `${path}/dist/css/${fileName}`;\n const pkgPath = nodePath.resolve(cssPath);\n\n if (nodeFs.existsSync(pkgPath)) {\n return nodeFs.readFileSync(pkgPath, \"utf8\");\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\ntype BuildTelegraphCssParams = {\n root: Root;\n cssFilePath?: string;\n config: {\n tokens: Array<\"light\" | \"dark\" | \"default\">;\n components: boolean;\n };\n};\n\n/**\n * Main function that builds the final CSS by:\n * 1. Getting all Telegraph dependencies\n * 2. Filtering for either token packages or component packages based on config\n * 3. Reading their CSS files\n * 4. Appending all CSS to the PostCSS Root node\n */\nconst buildTelegraphCss = async ({\n root,\n cssFilePath,\n config,\n}: BuildTelegraphCssParams) => {\n const deps = getTelegraphDeps(cssFilePath);\n\n const depsWithoutTokens =\n config.components === true\n ? Object.values(deps).filter(\n (dep) => !dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const tokensDeps =\n config.tokens.length > 0\n ? Object.values(deps).filter((dep) =>\n dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const cssFiles = depsWithoutTokens\n .map((dep) => getCssStyles({ path: dep.path }))\n .filter(Boolean) as Array<string>;\n\n const tokensCssFiles = config.tokens\n .map((token) => {\n const cssFile = tokensDeps.map((dep) =>\n getCssStyles({ path: dep.path, fileName: `${token}.css` }),\n );\n\n return cssFile.filter(Boolean) as Array<string>;\n })\n .filter(Boolean);\n\n const allCssFiles = [...cssFiles, ...tokensCssFiles];\n\n for (const content of allCssFiles) {\n const parsed = postcss.parse(content);\n root.append(parsed);\n }\n};\n\n/**\n * PostCSS plugin that processes @telegraph rules in CSS files.\n * It looks for @telegraph tokens, @telegraph tokens-light, @telegraph tokens-dark,\n * and @telegraph components rules and includes the appropriate CSS from Telegraph packages.\n */\nconst styleEnginePostCssPlugin = (): AcceptedPlugin => {\n return {\n postcssPlugin: \"@telegraph/style-engine\",\n plugins: [\n {\n postcssPlugin: \"telegraph\",\n AtRule: {\n telegraph() {},\n },\n async Once(root) {\n // Use the CSS file's path so getTelegraphDeps walks up from the\n // consumer package rather than from process.cwd() (which may be the\n // monorepo root when Turborepo is running the build).\n const cssFilePath = root.source?.input?.file;\n\n const run = {\n tokens: [] as BuildTelegraphCssParams[\"config\"][\"tokens\"],\n components: false,\n };\n root.walkAtRules(\"telegraph\", (atRule) => {\n if (atRule.params === \"components\") {\n run.components = true;\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-light\") {\n run.tokens.push(\"light\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-dark\") {\n run.tokens.push(\"dark\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens\") {\n run.tokens.push(\"default\");\n atRule.remove();\n }\n });\n\n await buildTelegraphCss({\n root,\n cssFilePath,\n config: {\n tokens: run.tokens,\n components: run.components,\n },\n });\n },\n },\n ],\n };\n};\n\nexport default Object.assign(styleEnginePostCssPlugin, {\n postcss: true,\n}) as PluginCreator<Record<string, never>>;\n"],"names":["nodePath","nodeFs","postcss","findNearestPackageJsonDir","start","current","pkgJsonPath","parent","findMonorepoRoot","run","getTelegraphDepsFromPackageJson","pkg","pkgPath","_a","acc","dep","version","getTelegraphDeps","cssFilePath","startDir","consumerDir","topLevelDeps","monorepoRoot","recursivelyGetTelegraphDeps","deps","allDeps","searchPath","localPkgJsonPath","rootPkgJsonPath","pkgJson","nestedDeps","getCssStyles","fileName","path","cssPath","buildTelegraphCss","root","config","depsWithoutTokens","tokensDeps","cssFiles","tokensCssFiles","token","allCssFiles","content","parsed","styleEnginePostCssPlugin","_b","atRule","postcss$1"],"mappings":"AAKA,MAAMA,IAAW,QAAQ,WAAW,GAE9BC,IAAS,QAAQ,SAAS,GAE1BC,IAAU,QAAQ,SAAS;AAqBjC,SAASC,EAA0BC,GAAmC;AACpE,MAAIC,IAAUD;AAEd,aAAa;AACX,UAAME,IAAcN,EAAS,KAAKK,GAAS,cAAc;AACzD,QAAIJ,EAAO,WAAWK,CAAW;AAC/B,aAAOD;AAGT,UAAME,IAASP,EAAS,QAAQK,CAAO;AACvC,QAAIE,MAAWF,EAAS;AACxB,IAAAA,IAAUE;AAAA,EACZ;AACF;AAMA,SAASC,EAAiBJ,IAAQ,QAAQ,OAA2B;AACnE,MAAIC,IAAUD,GACVK,IAAM;AAEV,SAAOA,KAAK;AACV,UAAMH,IAAcN,EAAS,KAAKK,GAAS,cAAc;AACzD,QAAIJ,EAAO,WAAWK,CAAW;AAC/B,UAAI;AAEF,YADY,KAAK,MAAML,EAAO,aAAaK,GAAa,MAAM,CAAC,EACvD;AACN,iBAAOD;AAAA,MAEX,QAAQ;AAAA,MAER;AAGF,UAAME,IAASP,EAAS,QAAQK,CAAO;AACvC,IAAIE,MAAWF,MAASI,IAAM,KAC9BJ,IAAUE;AAAA,EACZ;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O,WAAW,iBACpBvC,EAAI,aAAa,IACjBuC,EAAO,OAAA,IAGLA,EAAO,WAAW,mBACpBvC,EAAI,OAAO,KAAK,OAAO,GACvBuC,EAAO,OAAA,IAGLA,EAAO,WAAW,kBACpBvC,EAAI,OAAO,KAAK,MAAM,GACtBuC,EAAO,OAAA,IAGLA,EAAO,WAAW,aACpBvC,EAAI,OAAO,KAAK,SAAS,GACzBuC,EAAO,OAAA;AAAA,QAEX,CAAC,GAED,MAAMb,EAAkB;AAAA,UACtB,MAAAC;AAAA,UACA,aAAAlB;AAAA,UACA,QAAQ;AAAA,YACN,QAAQT,EAAI;AAAA,YACZ,YAAYA,EAAI;AAAA,UAAA;AAAA,QAClB,CACD;AAAA,MACH;AAAA,IAAA;AAAA,EACF;AACF,IAIJwC,IAAe,OAAO,OAAOH,GAA0B;AAAA,EACrD,SAAS;AACX,CAAC;"}
|
|
1
|
+
{"version":3,"file":"postcss.mjs","names":[],"sources":["../../__vite-browser-external","../../src/plugins/postcss.ts"],"sourcesContent":["module.exports = {}","import { type AcceptedPlugin, type PluginCreator, type Root } from \"postcss\";\n\n// Using require() instead of import to prevent ESM-related bugs in PostCSS.\n// ESM = ECMAScript Modules (the import/export syntax)\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodePath = require(\"node:path\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodeFs = require(\"node:fs\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst postcss = require(\"postcss\");\n\ntype PkgJson = {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n};\n\ntype DepObject = Record<\n string,\n {\n name: string;\n version: string;\n path: string;\n }\n>;\n\n/**\n * Traverses up the directory tree from a given start path to find the nearest package.json.\n * Returns the directory containing that package.json, or undefined if none is found.\n * Used to resolve the correct consumer package when process.cwd() may be the monorepo root.\n */\nfunction findNearestPackageJsonDir(start: string): string | undefined {\n let current = start;\n\n while (true) {\n const pkgJsonPath = nodePath.join(current, \"package.json\");\n if (nodeFs.existsSync(pkgJsonPath)) {\n return current;\n }\n\n const parent = nodePath.dirname(current);\n if (parent === current) return undefined;\n current = parent;\n }\n}\n\n/**\n * Traverses up the directory tree to find the root of the monorepo by looking for a package.json with workspaces.\n * This is needed to properly resolve workspace dependencies in a monorepo setup.\n */\nfunction findMonorepoRoot(start = process.cwd()): string | undefined {\n let current = start;\n let run = true;\n\n while (run) {\n const pkgJsonPath = nodePath.join(current, \"package.json\");\n if (nodeFs.existsSync(pkgJsonPath)) {\n try {\n const pkg = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n if (pkg.workspaces) {\n return current;\n }\n } catch {\n // malformed package.json, skip\n }\n }\n\n const parent = nodePath.dirname(current);\n if (parent === current) run = false;\n current = parent;\n }\n\n return undefined;\n}\n\n/**\n * Extracts all @telegraph/* dependencies from a package.json file and returns them with their paths.\n * This is used to find all Telegraph packages that might contain CSS we need to include.\n */\nconst getTelegraphDepsFromPackageJson = (\n pkg: PkgJson,\n pkgPath: string,\n): DepObject | undefined => {\n if (!pkg.dependencies || Object.keys(pkg.dependencies)?.length === 0) return;\n\n return Object.entries(pkg.dependencies).reduce((acc, [dep, version]) => {\n if (dep.startsWith(\"@telegraph/\")) {\n acc[dep] = {\n name: dep,\n version,\n path: nodePath.resolve(pkgPath, \"node_modules\", dep),\n };\n }\n return acc;\n }, {} as DepObject);\n};\n\n/**\n * Gets all Telegraph dependencies recursively, including dependencies of dependencies.\n * Handles both normal npm dependencies and workspace dependencies in a monorepo.\n *\n * @param cssFilePath - The absolute path of the CSS file being processed by PostCSS.\n * When provided, we walk up from the CSS file's directory to find the nearest\n * package.json. This ensures we read the *consumer's* package.json rather than\n * the monorepo root's package.json (which has no @telegraph/* deps), fixing the\n * Turborepo issue where process.cwd() is always the repo root during builds.\n */\nfunction getTelegraphDeps(cssFilePath?: string): DepObject {\n // Prefer to start from the CSS file's directory so we find the consumer's\n // package.json. Fall back to process.cwd() for in-memory PostCSS usage.\n const startDir = cssFilePath ? nodePath.dirname(cssFilePath) : process.cwd();\n const consumerDir = findNearestPackageJsonDir(startDir) ?? process.cwd();\n\n const pkgPath = nodePath.resolve(consumerDir, \"package.json\");\n const pkg = JSON.parse(nodeFs.readFileSync(pkgPath, \"utf8\"));\n const topLevelDeps = getTelegraphDepsFromPackageJson(pkg, consumerDir);\n const monorepoRoot = findMonorepoRoot(consumerDir);\n\n const recursivelyGetTelegraphDeps = (deps: DepObject): DepObject => {\n if (!deps || Object.keys(deps).length === 0) {\n return deps;\n }\n\n const allDeps = { ...deps };\n\n for (const dep of Object.values(deps)) {\n let pkgJsonPath: string;\n let searchPath: string;\n\n if (dep.version.includes(\"workspace:\") && monorepoRoot) {\n pkgJsonPath = nodePath.resolve(\n monorepoRoot,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n searchPath = monorepoRoot;\n // Workspace deps are hoisted to the monorepo root's node_modules.\n // The path stored in DepObject was set relative to consumerDir when the\n // dep was first recorded, so correct it to the monorepo root here so\n // that getCssStyles() can find the actual dist/css/default.css file.\n allDeps[dep.name] = {\n ...dep,\n path: nodePath.resolve(monorepoRoot, \"node_modules\", dep.name),\n };\n } else {\n // Non-workspace dep: try consumerDir/node_modules first, then fall\n // back to monorepoRoot/node_modules. Yarn (and other package managers)\n // hoist all deps to the root node_modules regardless of whether they\n // use workspace: protocol, so a pinned semver dep like \"0.1.1\" will\n // typically live at the monorepo root, not the consumer package dir.\n const localPkgJsonPath = nodePath.resolve(\n consumerDir,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n const rootPkgJsonPath =\n monorepoRoot &&\n nodePath.resolve(\n monorepoRoot,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n\n if (nodeFs.existsSync(localPkgJsonPath)) {\n pkgJsonPath = localPkgJsonPath;\n searchPath = consumerDir;\n } else if (rootPkgJsonPath && nodeFs.existsSync(rootPkgJsonPath)) {\n pkgJsonPath = rootPkgJsonPath;\n searchPath = monorepoRoot!;\n // Correct the stored path so getCssStyles() finds the right dist/css/ dir.\n allDeps[dep.name] = {\n ...dep,\n path: nodePath.resolve(monorepoRoot!, \"node_modules\", dep.name),\n };\n } else {\n pkgJsonPath = localPkgJsonPath; // will throw on readFileSync, caught below\n searchPath = consumerDir;\n }\n }\n\n try {\n const pkgJson = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n const deps = getTelegraphDepsFromPackageJson(pkgJson, searchPath);\n\n // Merge new dependencies into allDeps\n Object.assign(allDeps, deps);\n\n if (deps) {\n // Recursively get dependencies of dependencies\n const nestedDeps = recursivelyGetTelegraphDeps(deps);\n Object.assign(allDeps, nestedDeps);\n }\n } catch (_err) {\n // Skip if package.json cannot be read\n continue;\n }\n }\n\n return allDeps;\n };\n\n if (topLevelDeps) {\n return recursivelyGetTelegraphDeps(topLevelDeps);\n }\n\n return {};\n}\n\ntype GetCssStylesParams = {\n fileName?: string;\n path: string;\n};\n\n/**\n * Reads CSS file content from a Telegraph package's dist/css directory.\n * Returns null if the file doesn't exist or can't be read.\n */\nfunction getCssStyles({\n fileName = \"default.css\",\n path,\n}: GetCssStylesParams): string | null {\n try {\n const cssPath = `${path}/dist/css/${fileName}`;\n const pkgPath = nodePath.resolve(cssPath);\n\n if (nodeFs.existsSync(pkgPath)) {\n return nodeFs.readFileSync(pkgPath, \"utf8\");\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\ntype BuildTelegraphCssParams = {\n root: Root;\n cssFilePath?: string;\n config: {\n tokens: Array<\"light\" | \"dark\" | \"default\">;\n components: boolean;\n };\n};\n\n/**\n * Main function that builds the final CSS by:\n * 1. Getting all Telegraph dependencies\n * 2. Filtering for either token packages or component packages based on config\n * 3. Reading their CSS files\n * 4. Appending all CSS to the PostCSS Root node\n */\nconst buildTelegraphCss = async ({\n root,\n cssFilePath,\n config,\n}: BuildTelegraphCssParams) => {\n const deps = getTelegraphDeps(cssFilePath);\n\n const depsWithoutTokens =\n config.components === true\n ? Object.values(deps).filter(\n (dep) => !dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const tokensDeps =\n config.tokens.length > 0\n ? Object.values(deps).filter((dep) =>\n dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const cssFiles = depsWithoutTokens\n .map((dep) => getCssStyles({ path: dep.path }))\n .filter(Boolean) as Array<string>;\n\n const tokensCssFiles = config.tokens\n .map((token) => {\n const cssFile = tokensDeps.map((dep) =>\n getCssStyles({ path: dep.path, fileName: `${token}.css` }),\n );\n\n return cssFile.filter(Boolean) as Array<string>;\n })\n .filter(Boolean);\n\n const allCssFiles = [...cssFiles, ...tokensCssFiles];\n\n for (const content of allCssFiles) {\n const parsed = postcss.parse(content);\n root.append(parsed);\n }\n};\n\n/**\n * PostCSS plugin that processes @telegraph rules in CSS files.\n * It looks for @telegraph tokens, @telegraph tokens-light, @telegraph tokens-dark,\n * and @telegraph components rules and includes the appropriate CSS from Telegraph packages.\n */\nconst styleEnginePostCssPlugin = (): AcceptedPlugin => {\n return {\n postcssPlugin: \"@telegraph/style-engine\",\n plugins: [\n {\n postcssPlugin: \"telegraph\",\n AtRule: {\n telegraph() {},\n },\n async Once(root) {\n // Use the CSS file's path so getTelegraphDeps walks up from the\n // consumer package rather than from process.cwd() (which may be the\n // monorepo root when Turborepo is running the build).\n const cssFilePath = root.source?.input?.file;\n\n const run = {\n tokens: [] as BuildTelegraphCssParams[\"config\"][\"tokens\"],\n components: false,\n };\n root.walkAtRules(\"telegraph\", (atRule) => {\n if (atRule.params === \"components\") {\n run.components = true;\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-light\") {\n run.tokens.push(\"light\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-dark\") {\n run.tokens.push(\"dark\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens\") {\n run.tokens.push(\"default\");\n atRule.remove();\n }\n });\n\n await buildTelegraphCss({\n root,\n cssFilePath,\n config: {\n tokens: run.tokens,\n components: run.components,\n },\n });\n },\n },\n ],\n };\n};\n\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,OAAO,KAAK,UAAU,EAC1B,EAAO,QAAQ;KAEjB,EAEF,MAAM,EAAkB;IACtB;IACA;IACA,QAAQ;KACN,QAAQ,EAAI;KACZ,YAAY,EAAI;KACjB;IACF,CAAC;;EAEL,CACF;CACF,GAGoD,EACrD,SAAS,IACV,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telegraph/style-engine",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "A wrappar around vanilla extract to style telegraph",
|
|
5
5
|
"repository": "https://github.com/knocklabs/telegraph/tree/main/packages/style-engine",
|
|
6
6
|
"author": "@knocklabs",
|
|
@@ -38,23 +38,23 @@
|
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@telegraph/tokens": "^0.2.0",
|
|
41
|
-
"postcss": "^8.5.
|
|
41
|
+
"postcss": "^8.5.13"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@knocklabs/eslint-config": "^0.0.5",
|
|
45
45
|
"@knocklabs/typescript-config": "^0.0.2",
|
|
46
|
-
"@telegraph/prettier-config": "^0.0.
|
|
46
|
+
"@telegraph/prettier-config": "^0.0.8",
|
|
47
47
|
"@telegraph/vite-config": "^0.0.15",
|
|
48
|
-
"@types/node": "^25.
|
|
48
|
+
"@types/node": "^25.6.0",
|
|
49
49
|
"@types/postcss-import": "^14.0.3",
|
|
50
|
-
"@types/react": "^19.2.
|
|
51
|
-
"eslint": "^10.2.
|
|
52
|
-
"globby": "^16.
|
|
50
|
+
"@types/react": "^19.2.14",
|
|
51
|
+
"eslint": "^10.2.1",
|
|
52
|
+
"globby": "^16.2.0",
|
|
53
53
|
"lightningcss": "^1.32.0",
|
|
54
|
-
"react": "^19.2.
|
|
55
|
-
"react-dom": "^19.2.
|
|
56
|
-
"typescript": "^
|
|
57
|
-
"vite": "^
|
|
54
|
+
"react": "^19.2.6",
|
|
55
|
+
"react-dom": "^19.2.6",
|
|
56
|
+
"typescript": "^6.0.2",
|
|
57
|
+
"vite": "^8.0.0"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
60
|
"react": "^18.0.0 || ^19.0.0",
|