@backstage/ui 0.11.2 → 0.12.0-next.1
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 +304 -108
- package/css/styles.css +25 -19
- package/dist/components/Alert/Alert.esm.js +81 -0
- package/dist/components/Alert/Alert.esm.js.map +1 -0
- package/dist/components/Alert/Alert.module.css.esm.js +8 -0
- package/dist/components/Alert/Alert.module.css.esm.js.map +1 -0
- package/dist/components/Alert/definition.esm.js +37 -0
- package/dist/components/Alert/definition.esm.js.map +1 -0
- package/dist/components/Box/Box.esm.js +10 -23
- package/dist/components/Box/Box.esm.js.map +1 -1
- package/dist/components/Box/definition.esm.js +20 -6
- package/dist/components/Box/definition.esm.js.map +1 -1
- package/dist/components/Button/Button.esm.js +12 -35
- package/dist/components/Button/Button.esm.js.map +1 -1
- package/dist/components/Button/Button.module.css.esm.js +3 -3
- package/dist/components/Button/definition.esm.js +23 -6
- package/dist/components/Button/definition.esm.js.map +1 -1
- package/dist/components/ButtonIcon/ButtonIcon.esm.js +9 -40
- package/dist/components/ButtonIcon/ButtonIcon.esm.js.map +1 -1
- package/dist/components/ButtonIcon/ButtonIcon.module.css.esm.js +3 -3
- package/dist/components/ButtonIcon/definition.esm.js +21 -2
- package/dist/components/ButtonIcon/definition.esm.js.map +1 -1
- package/dist/components/ButtonLink/ButtonLink.esm.js +16 -43
- package/dist/components/ButtonLink/ButtonLink.esm.js.map +1 -1
- package/dist/components/ButtonLink/ButtonLink.module.css.esm.js +8 -0
- package/dist/components/ButtonLink/ButtonLink.module.css.esm.js.map +1 -0
- package/dist/components/ButtonLink/definition.esm.js +24 -3
- package/dist/components/ButtonLink/definition.esm.js.map +1 -1
- package/dist/components/Dialog/Dialog.esm.js +3 -0
- package/dist/components/Dialog/Dialog.esm.js.map +1 -1
- package/dist/components/HeaderPage/HeaderPage.esm.js +1 -0
- package/dist/components/HeaderPage/HeaderPage.esm.js.map +1 -1
- package/dist/components/InternalLinkProvider/InternalLinkProvider.esm.js +55 -0
- package/dist/components/InternalLinkProvider/InternalLinkProvider.esm.js.map +1 -0
- package/dist/components/Link/Link.esm.js +3 -9
- package/dist/components/Link/Link.esm.js.map +1 -1
- package/dist/components/Link/Link.module.css.esm.js +2 -2
- package/dist/components/Link/definition.esm.js +10 -2
- package/dist/components/Link/definition.esm.js.map +1 -1
- package/dist/components/Menu/Menu.esm.js +13 -16
- package/dist/components/Menu/Menu.esm.js.map +1 -1
- package/dist/components/Popover/Popover.module.css.esm.js +2 -2
- package/dist/components/Switch/Switch.module.css.esm.js +2 -2
- package/dist/components/Table/components/Row.esm.js +5 -18
- package/dist/components/Table/components/Row.esm.js.map +1 -1
- package/dist/components/Table/components/Table.esm.js +4 -4
- package/dist/components/Table/components/Table.esm.js.map +1 -1
- package/dist/components/TablePagination/TablePagination.esm.js +4 -1
- package/dist/components/TablePagination/TablePagination.esm.js.map +1 -1
- package/dist/components/Tabs/Tabs.esm.js +120 -54
- package/dist/components/Tabs/Tabs.esm.js.map +1 -1
- package/dist/components/TagGroup/TagGroup.esm.js +7 -13
- package/dist/components/TagGroup/TagGroup.esm.js.map +1 -1
- package/dist/components/Text/Text.module.css.esm.js +2 -2
- package/dist/components/Text/definition.esm.js +8 -1
- package/dist/components/Text/definition.esm.js.map +1 -1
- package/dist/components/Tooltip/Tooltip.module.css.esm.js +2 -2
- package/dist/hooks/useDefinition/defineComponent.esm.js +6 -0
- package/dist/hooks/useDefinition/defineComponent.esm.js.map +1 -0
- package/dist/hooks/useDefinition/helpers.esm.js +69 -0
- package/dist/hooks/useDefinition/helpers.esm.js.map +1 -0
- package/dist/hooks/useDefinition/useDefinition.esm.js +76 -0
- package/dist/hooks/useDefinition/useDefinition.esm.js.map +1 -0
- package/dist/hooks/useStyles.esm.js +7 -54
- package/dist/hooks/useStyles.esm.js.map +1 -1
- package/dist/index.d.ts +322 -107
- package/dist/index.esm.js +4 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/utils/utilityClassMap.esm.js.map +1 -1
- package/package.json +6 -6
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import clsx from 'clsx';
|
|
3
|
+
import { useBreakpoint } from '../useBreakpoint.esm.js';
|
|
4
|
+
import { useSurface, SurfaceProvider } from '../useSurface.esm.js';
|
|
5
|
+
import { resolveResponsiveValue, processUtilityProps } from './helpers.esm.js';
|
|
6
|
+
|
|
7
|
+
function useDefinition(definition, props, options) {
|
|
8
|
+
const { breakpoint } = useBreakpoint();
|
|
9
|
+
const surfaceOptions = definition.surface === "container" ? { surface: props.surface } : definition.surface === "leaf" ? { onSurface: props.onSurface } : void 0;
|
|
10
|
+
const { surface: resolvedSurface } = useSurface(surfaceOptions);
|
|
11
|
+
const ownPropKeys = new Set(Object.keys(definition.propDefs));
|
|
12
|
+
const utilityPropKeys = new Set(definition.utilityProps ?? []);
|
|
13
|
+
const ownPropsRaw = {};
|
|
14
|
+
const restProps = {};
|
|
15
|
+
for (const [key, value] of Object.entries(props)) {
|
|
16
|
+
if (ownPropKeys.has(key)) {
|
|
17
|
+
ownPropsRaw[key] = value;
|
|
18
|
+
} else if (!utilityPropKeys.has(key)) {
|
|
19
|
+
restProps[key] = value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const ownPropsResolved = {};
|
|
23
|
+
const dataAttributes = {};
|
|
24
|
+
for (const [key, config] of Object.entries(definition.propDefs)) {
|
|
25
|
+
const rawValue = ownPropsRaw[key];
|
|
26
|
+
const resolvedValue = resolveResponsiveValue(rawValue, breakpoint);
|
|
27
|
+
const finalValue = resolvedValue ?? config.default;
|
|
28
|
+
if (finalValue !== void 0) {
|
|
29
|
+
ownPropsResolved[key] = finalValue;
|
|
30
|
+
if (config.dataAttribute) {
|
|
31
|
+
dataAttributes[`data-${key.toLowerCase()}`] = String(finalValue);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (definition.surface === "leaf" && resolvedSurface !== void 0) {
|
|
36
|
+
const surfaceValue = typeof resolvedSurface === "object" ? resolveResponsiveValue(resolvedSurface, breakpoint) : resolvedSurface;
|
|
37
|
+
if (surfaceValue !== void 0) {
|
|
38
|
+
dataAttributes["data-on-surface"] = String(surfaceValue);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const { utilityClasses, utilityStyle } = processUtilityProps(
|
|
42
|
+
props,
|
|
43
|
+
definition.utilityProps ?? []
|
|
44
|
+
);
|
|
45
|
+
const utilityTarget = "root";
|
|
46
|
+
const classNameTarget = "root";
|
|
47
|
+
const classes = {};
|
|
48
|
+
for (const [name, cssKey] of Object.entries(definition.classNames)) {
|
|
49
|
+
classes[name] = clsx(
|
|
50
|
+
cssKey,
|
|
51
|
+
definition.styles[cssKey],
|
|
52
|
+
utilityTarget === name && utilityClasses,
|
|
53
|
+
classNameTarget === name && ownPropsResolved.className
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
let children;
|
|
57
|
+
let surfaceChildren;
|
|
58
|
+
if (definition.surface === "container") {
|
|
59
|
+
surfaceChildren = resolvedSurface ? /* @__PURE__ */ jsx(SurfaceProvider, { surface: resolvedSurface, children: props.children }) : props.children;
|
|
60
|
+
} else {
|
|
61
|
+
children = props.children;
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
ownProps: {
|
|
65
|
+
classes,
|
|
66
|
+
...ownPropsResolved,
|
|
67
|
+
...definition.surface === "container" ? { surfaceChildren } : { children }
|
|
68
|
+
},
|
|
69
|
+
restProps,
|
|
70
|
+
dataAttributes,
|
|
71
|
+
utilityStyle
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { useDefinition };
|
|
76
|
+
//# sourceMappingURL=useDefinition.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDefinition.esm.js","sources":["../../../src/hooks/useDefinition/useDefinition.tsx"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ReactNode } from 'react';\nimport clsx from 'clsx';\nimport { useBreakpoint } from '../useBreakpoint';\nimport { useSurface, SurfaceProvider, UseSurfaceOptions } from '../useSurface';\nimport { resolveResponsiveValue, processUtilityProps } from './helpers';\nimport type {\n ComponentConfig,\n UseDefinitionOptions,\n UseDefinitionResult,\n UtilityKeys,\n} from './types';\n\nexport function useDefinition<\n D extends ComponentConfig<any, any>,\n P extends Record<string, any>,\n>(\n definition: D,\n props: P,\n options?: UseDefinitionOptions<D>,\n): UseDefinitionResult<D, P> {\n const { breakpoint } = useBreakpoint();\n\n const surfaceOptions: UseSurfaceOptions | undefined =\n definition.surface === 'container'\n ? { surface: props.surface }\n : definition.surface === 'leaf'\n ? { onSurface: props.onSurface }\n : undefined;\n\n const { surface: resolvedSurface } = useSurface(surfaceOptions);\n\n const ownPropKeys = new Set(Object.keys(definition.propDefs));\n const utilityPropKeys = new Set(definition.utilityProps ?? []);\n\n const ownPropsRaw: Record<string, any> = {};\n const restProps: Record<string, any> = {};\n\n for (const [key, value] of Object.entries(props)) {\n if (ownPropKeys.has(key)) {\n ownPropsRaw[key] = value;\n } else if (!(utilityPropKeys as Set<string>).has(key)) {\n restProps[key] = value;\n }\n }\n\n const ownPropsResolved: Record<string, any> = {};\n const dataAttributes: Record<string, string | undefined> = {};\n\n for (const [key, config] of Object.entries(definition.propDefs)) {\n const rawValue = ownPropsRaw[key];\n const resolvedValue = resolveResponsiveValue(rawValue, breakpoint);\n const finalValue = resolvedValue ?? (config as any).default;\n\n if (finalValue !== undefined) {\n ownPropsResolved[key] = finalValue;\n\n if ((config as any).dataAttribute) {\n // eslint-disable-next-line no-restricted-syntax\n dataAttributes[`data-${key.toLowerCase()}`] = String(finalValue);\n }\n }\n }\n\n // Add data-on-surface for leaf components\n if (definition.surface === 'leaf' && resolvedSurface !== undefined) {\n // Handle responsive surface values - for data attributes, use the resolved string\n const surfaceValue =\n typeof resolvedSurface === 'object'\n ? resolveResponsiveValue(resolvedSurface as any, breakpoint)\n : resolvedSurface;\n if (surfaceValue !== undefined) {\n dataAttributes['data-on-surface'] = String(surfaceValue);\n }\n }\n\n const { utilityClasses, utilityStyle } = processUtilityProps<UtilityKeys<D>>(\n props,\n (definition.utilityProps ?? []) as readonly UtilityKeys<D>[],\n );\n\n const utilityTarget = options?.utilityTarget ?? 'root';\n const classNameTarget = options?.classNameTarget ?? 'root';\n\n const classes: Record<string, string> = {};\n\n for (const [name, cssKey] of Object.entries(definition.classNames)) {\n classes[name] = clsx(\n cssKey as string,\n definition.styles[cssKey as keyof typeof definition.styles],\n utilityTarget === name && utilityClasses,\n classNameTarget === name && ownPropsResolved.className,\n );\n }\n\n let children: ReactNode | undefined;\n let surfaceChildren: ReactNode | undefined;\n\n if (definition.surface === 'container') {\n surfaceChildren = resolvedSurface ? (\n <SurfaceProvider surface={resolvedSurface}>\n {props.children}\n </SurfaceProvider>\n ) : (\n props.children\n );\n } else {\n children = props.children;\n }\n\n return {\n ownProps: {\n classes,\n ...ownPropsResolved,\n ...(definition.surface === 'container'\n ? { surfaceChildren }\n : { children }),\n },\n restProps,\n dataAttributes,\n utilityStyle,\n } as unknown as UseDefinitionResult<D, P>;\n}\n"],"names":[],"mappings":";;;;;;AA4BO,SAAS,aAAA,CAId,UAAA,EACA,KAAA,EACA,OAAA,EAC2B;AAC3B,EAAA,MAAM,EAAE,UAAA,EAAW,GAAI,aAAA,EAAc;AAErC,EAAA,MAAM,iBACJ,UAAA,CAAW,OAAA,KAAY,WAAA,GACnB,EAAE,SAAS,KAAA,CAAM,OAAA,E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AA;AAAA,EAEV,CAAA,MAAO;AACL,IAAA,QAAA,GAAW,KAAA,CAAM,QAAA;AAAA,EACnB;AAEA,EAAA,OAAO;AAAA,IACL,QAAA,EAAU;AAAA,MACR,OAAA;AAAA,MACA,GAAG,gBAAA;AAAA,MACH,GAAI,WAAW,OAAA,KAAY,WAAA,GACvB,EAAE,eAAA,EAAgB,GAClB,EAAE,QAAA;AAAS,KACjB;AAAA,IACA,SAAA;AAAA,IACA,cAAA;AAAA,IACA;AAAA,GACF;AACF;;;;"}
|
|
@@ -1,25 +1,6 @@
|
|
|
1
|
-
import { useBreakpoint
|
|
2
|
-
import {
|
|
1
|
+
import { useBreakpoint } from './useBreakpoint.esm.js';
|
|
2
|
+
import { resolveResponsiveValue, processUtilityProps } from './useDefinition/helpers.esm.js';
|
|
3
3
|
|
|
4
|
-
function resolveResponsiveValue(value, breakpoint) {
|
|
5
|
-
if (typeof value === "string") {
|
|
6
|
-
return value;
|
|
7
|
-
}
|
|
8
|
-
if (typeof value === "object" && value !== null) {
|
|
9
|
-
const index = breakpoints.findIndex((b) => b.id === breakpoint);
|
|
10
|
-
for (let i = index; i >= 0; i--) {
|
|
11
|
-
if (value[breakpoints[i].id]) {
|
|
12
|
-
return value[breakpoints[i].id];
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
for (let i = 0; i < breakpoints.length; i++) {
|
|
16
|
-
if (value[breakpoints[i].id]) {
|
|
17
|
-
return value[breakpoints[i].id];
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return void 0;
|
|
22
|
-
}
|
|
23
4
|
function useStyles(componentDefinition, props = {}) {
|
|
24
5
|
const { breakpoint } = useBreakpoint();
|
|
25
6
|
const classNames = componentDefinition.classNames;
|
|
@@ -40,38 +21,10 @@ function useStyles(componentDefinition, props = {}) {
|
|
|
40
21
|
}
|
|
41
22
|
}
|
|
42
23
|
}
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (!utilityConfig) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
if (utilityConfig.values.length > 0 && utilityConfig.values.includes(val)) {
|
|
51
|
-
const className = prefix ? `${prefix}${utilityConfig.class}-${val}` : `${utilityConfig.class}-${val}`;
|
|
52
|
-
utilityClassList.push(className);
|
|
53
|
-
} else if (utilityConfig.cssVar) {
|
|
54
|
-
const cssVarKey = prefix ? `${utilityConfig.cssVar}-${prefix.slice(0, -1)}` : utilityConfig.cssVar;
|
|
55
|
-
generatedStyle[cssVarKey] = val;
|
|
56
|
-
const className = prefix ? `${prefix}${utilityConfig.class}` : utilityConfig.class;
|
|
57
|
-
utilityClassList.push(className);
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
for (const key of utilityPropNames) {
|
|
61
|
-
const value = props[key];
|
|
62
|
-
if (value === void 0 || value === null) {
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
if (typeof value === "object" && value !== null) {
|
|
66
|
-
const breakpointValues = value;
|
|
67
|
-
for (const bp in breakpointValues) {
|
|
68
|
-
const prefix = bp === "initial" ? "" : `${bp}:`;
|
|
69
|
-
handleUtilityValue(key, breakpointValues[bp], prefix);
|
|
70
|
-
}
|
|
71
|
-
} else {
|
|
72
|
-
handleUtilityValue(key, value);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
24
|
+
const { utilityClasses, utilityStyle: generatedStyle } = processUtilityProps(
|
|
25
|
+
props,
|
|
26
|
+
utilityPropNames
|
|
27
|
+
);
|
|
75
28
|
const utilityPropsSet = new Set(utilityPropNames);
|
|
76
29
|
const cleanedPropsBase = Object.keys(props).reduce((acc, key) => {
|
|
77
30
|
if (!utilityPropsSet.has(key)) {
|
|
@@ -90,7 +43,7 @@ function useStyles(componentDefinition, props = {}) {
|
|
|
90
43
|
return {
|
|
91
44
|
classNames,
|
|
92
45
|
dataAttributes,
|
|
93
|
-
utilityClasses
|
|
46
|
+
utilityClasses,
|
|
94
47
|
style: mergedStyle,
|
|
95
48
|
cleanedProps
|
|
96
49
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStyles.esm.js","sources":["../../src/hooks/useStyles.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { useBreakpoint, breakpoints } from './useBreakpoint';\nimport type { ComponentDefinition } from '../types';\nimport { utilityClassMap } from '../utils/utilityClassMap';\n\n/**\n * Resolve a responsive value based on the current breakpoint\n * @param value - The responsive value (string or object with breakpoint keys)\n * @param breakpoint - The current breakpoint\n * @returns The resolved value for the current breakpoint\n */\nfunction resolveResponsiveValue(\n value: string | Record<string, string>,\n breakpoint: string,\n): string | undefined {\n if (typeof value === 'string') {\n return value;\n }\n\n if (typeof value === 'object' && value !== null) {\n const index = breakpoints.findIndex(b => b.id === breakpoint);\n\n // Look for value at current breakpoint or smaller\n for (let i = index; i >= 0; i--) {\n if (value[breakpoints[i].id]) {\n return value[breakpoints[i].id];\n }\n }\n\n // If no value found, check from smallest breakpoint up\n for (let i = 0; i < breakpoints.length; i++) {\n if (value[breakpoints[i].id]) {\n return value[breakpoints[i].id];\n }\n }\n }\n\n return undefined;\n}\n\n/**\n * React hook to get class names and data attributes for a component with responsive support\n * @param componentDefinition - The component's definition object\n * @param props - All component props\n * @returns Object with classNames, dataAttributes, utilityClasses, style, and cleanedProps\n */\nexport function useStyles<\n T extends ComponentDefinition,\n P extends Record<string, any> = Record<string, any>,\n>(\n componentDefinition: T,\n props: P = {} as P,\n): {\n classNames: T['classNames'];\n dataAttributes: Record<string, string>;\n utilityClasses: string;\n style: React.CSSProperties;\n cleanedProps: P;\n} {\n const { breakpoint } = useBreakpoint();\n const classNames = componentDefinition.classNames;\n const utilityPropNames =\n ('utilityProps' in componentDefinition\n ? componentDefinition.utilityProps\n : []) || [];\n\n // Extract data attribute names from component definition\n const dataAttributeNames =\n 'dataAttributes' in componentDefinition\n ? Object.keys(componentDefinition.dataAttributes || {})\n : [];\n\n // Extract existing style from props\n const incomingStyle = props.style || {};\n\n // Generate data attributes from component definition\n const dataAttributes: Record<string, string> = {};\n for (const key of dataAttributeNames) {\n const value = props[key];\n if (value !== undefined && value !== null) {\n // Handle boolean and number values directly\n if (typeof value === 'boolean' || typeof value === 'number') {\n dataAttributes[`data-${key}`] = String(value);\n } else {\n const resolvedValue = resolveResponsiveValue(value, breakpoint);\n if (resolvedValue !== undefined) {\n dataAttributes[`data-${key}`] = resolvedValue;\n }\n }\n }\n }\n\n // Generate utility classes and custom styles from component's allowed utility props\n const utilityClassList: string[] = [];\n const generatedStyle: React.CSSProperties = {};\n\n const handleUtilityValue = (\n key: string,\n val: unknown,\n prefix: string = '',\n ) => {\n // Get utility class configuration for this key\n const utilityConfig = utilityClassMap[key as keyof typeof utilityClassMap];\n\n if (!utilityConfig) {\n // Skip if no config found for this key\n return;\n }\n\n // Check if value is in the list of valid values for this utility\n if (\n utilityConfig.values.length > 0 &&\n utilityConfig.values.includes(val as string | number)\n ) {\n // Generate utility class with value suffix and optional breakpoint prefix\n const className = prefix\n ? `${prefix}${utilityConfig.class}-${val}`\n : `${utilityConfig.class}-${val}`;\n utilityClassList.push(className);\n } else if (utilityConfig.cssVar) {\n // Custom value - add CSS custom property AND utility class name\n // Only if cssVar is defined (properties with fixed values don't have cssVar)\n const cssVarKey = prefix\n ? `${utilityConfig.cssVar}-${prefix.slice(0, -1)}`\n : utilityConfig.cssVar;\n // CSS custom properties need to be set on the style object as strings\n (generatedStyle as Record<string, unknown>)[cssVarKey] = val;\n\n // Add utility class name (without value suffix) with optional breakpoint prefix\n const className = prefix\n ? `${prefix}${utilityConfig.class}`\n : utilityConfig.class;\n utilityClassList.push(className);\n }\n // If no cssVar and value is not in valid values, skip (invalid value for fixed-value property)\n };\n\n for (const key of utilityPropNames) {\n const value = props[key];\n if (value === undefined || value === null) {\n continue;\n }\n\n // Check if value is a responsive object\n if (typeof value === 'object' && value !== null) {\n const breakpointValues = value as { [key: string]: unknown };\n // Handle responsive object values\n for (const bp in breakpointValues) {\n const prefix = bp === 'initial' ? '' : `${bp}:`;\n handleUtilityValue(key, breakpointValues[bp], prefix);\n }\n } else {\n // Handle simple value\n handleUtilityValue(key, value);\n }\n }\n\n // Create cleaned props by excluding only utility props\n // All other props (including data attributes, style, children, etc.) remain\n const utilityPropsSet = new Set<string>(utilityPropNames);\n\n const cleanedPropsBase = Object.keys(props).reduce((acc, key) => {\n if (!utilityPropsSet.has(key)) {\n acc[key] = props[key];\n }\n return acc;\n }, {} as any);\n\n // Merge incoming style with generated styles (incoming styles take precedence)\n const mergedStyle = {\n ...generatedStyle,\n ...incomingStyle,\n };\n\n // Add merged style to cleanedProps\n const cleanedProps = {\n ...cleanedPropsBase,\n style: mergedStyle,\n } as P;\n\n return {\n classNames,\n dataAttributes,\n utilityClasses: utilityClassList.join(' '),\n style: mergedStyle,\n cleanedProps,\n };\n}\n"],"names":[],"mappings":";;;AAyBA,SAAS,sBAAA,CACP,OACA,UAAA,EACoB;AACpB,EAAA,IAAI,OAAO,UAAU,QAAA,EAAU;AAC7B,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,IAAI,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,KAAU,IAAA,EAAM;AAC/C,IAAA,MAAM,QAAQ,WAAA,CAAY,SAAA,CAAU,CAAA,CAAA,KAAK,CAAA,CAAE,OAAO,UAAU,CAAA;AAG5D,IAAA,KAAA,IAAS,CAAA,GAAI,KAAA,EAAO,CAAA,IAAK,CAAA,EAAG,CAAA,EAAA,EAAK;AAC/B,MAAA,IAAI,KAAA,CAAM,WA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AAO;AAAA,GACT;AAEA,EAAA,OAAO;AAAA,IACL,UAAA;AAAA,IACA,cAAA;AAAA,IACA,cAAA,EAAgB,gBAAA,CAAiB,IAAA,CAAK,GAAG,CAAA;AAAA,IACzC,KAAA,EAAO,WAAA;AAAA,IACP;AAAA,GACF;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"useStyles.esm.js","sources":["../../src/hooks/useStyles.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { useBreakpoint } from './useBreakpoint';\nimport type { ComponentDefinition } from '../types';\nimport {\n resolveResponsiveValue,\n processUtilityProps,\n} from './useDefinition/helpers';\n\n/**\n * React hook to get class names and data attributes for a component with responsive support\n * @param componentDefinition - The component's definition object\n * @param props - All component props\n * @returns Object with classNames, dataAttributes, utilityClasses, style, and cleanedProps\n */\nexport function useStyles<\n T extends ComponentDefinition,\n P extends Record<string, any> = Record<string, any>,\n>(\n componentDefinition: T,\n props: P = {} as P,\n): {\n classNames: T['classNames'];\n dataAttributes: Record<string, string>;\n utilityClasses: string;\n style: React.CSSProperties;\n cleanedProps: P;\n} {\n const { breakpoint } = useBreakpoint();\n const classNames = componentDefinition.classNames;\n const utilityPropNames =\n ('utilityProps' in componentDefinition\n ? componentDefinition.utilityProps\n : []) || [];\n\n // Extract data attribute names from component definition\n const dataAttributeNames =\n 'dataAttributes' in componentDefinition\n ? Object.keys(componentDefinition.dataAttributes || {})\n : [];\n\n // Extract existing style from props\n const incomingStyle = props.style || {};\n\n // Generate data attributes from component definition\n const dataAttributes: Record<string, string> = {};\n for (const key of dataAttributeNames) {\n const value = props[key];\n if (value !== undefined && value !== null) {\n // Handle boolean and number values directly\n if (typeof value === 'boolean' || typeof value === 'number') {\n dataAttributes[`data-${key}`] = String(value);\n } else {\n const resolvedValue = resolveResponsiveValue(value, breakpoint);\n if (resolvedValue !== undefined) {\n dataAttributes[`data-${key}`] = resolvedValue;\n }\n }\n }\n }\n\n // Generate utility classes and custom styles from component's allowed utility props\n const { utilityClasses, utilityStyle: generatedStyle } = processUtilityProps(\n props,\n utilityPropNames,\n );\n\n // Create cleaned props by excluding only utility props\n // All other props (including data attributes, style, children, etc.) remain\n const utilityPropsSet = new Set<string>(utilityPropNames);\n\n const cleanedPropsBase = Object.keys(props).reduce((acc, key) => {\n if (!utilityPropsSet.has(key)) {\n acc[key] = props[key];\n }\n return acc;\n }, {} as any);\n\n // Merge incoming style with generated styles (incoming styles take precedence)\n const mergedStyle = {\n ...generatedStyle,\n ...incomingStyle,\n };\n\n // Add merged style to cleanedProps\n const cleanedProps = {\n ...cleanedPropsBase,\n style: mergedStyle,\n } as P;\n\n return {\n classNames,\n dataAttributes,\n utilityClasses,\n style: mergedStyle,\n cleanedProps,\n };\n}\n"],"names":[],"mappings":";;;AA4BO,SAAS,SAAA,CAId,mBAAA,EACA,KAAA,GAAW,EAAC,EAOZ;AACA,EAAA,MAAM,EAAE,UAAA,EAAW,GAAI,aAAA,EAAc;AACrC,EAAA,MAAM,aAAa,mBAAA,CAAoB,UAAA;AACvC,EAAA,MAAM,oBACH,cAAA,IAAkB,mBAAA,GACf,oBAAoB,YAAA,GACpB,OAAO,EAAC;AAGd,EAAA,MAAM,kBAAA,GACJ,gBAAA,IAAoB,mBAAA,GAChB,MAAA,CAAO,IAAA,CAAK,oBAAoB,cAAA,IAAkB,EAAE,CAAA,GACpD,EAAC;AAGP,EAAA,MAAM,aAAA,GAAgB,KAAA,CAAM,KAAA,IAAS,EAAC;AAGtC,EAAA,MAAM,iBAAyC,EAAC;AAChD,EAAA,KAAA,MAAW,OAAO,kB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