@backstage/ui 0.11.0 → 0.12.0-next.0
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 +226 -104
- package/css/styles.css +17 -11
- 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 +2 -2
- package/dist/components/Button/definition.esm.js +22 -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/InternalLinkProvider/InternalLinkProvider.esm.js +55 -0
- package/dist/components/InternalLinkProvider/InternalLinkProvider.esm.js.map +1 -0
- package/dist/components/Link/Link.esm.js +2 -9
- package/dist/components/Link/Link.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/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 +5 -4
- package/dist/components/Table/components/Table.esm.js.map +1 -1
- package/dist/components/TablePagination/TablePagination.esm.js +5 -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/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 +78 -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 +313 -104
- 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 +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Tabs.esm.js","sources":["../../../src/components/Tabs/Tabs.tsx"],"sourcesContent":["/*\n * Copyright 2024 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 {\n useRef,\n useState,\n Children,\n cloneElement,\n isValidElement,\n ReactNode,\n createContext,\n useContext,\n} from 'react';\nimport type {\n TabsProps,\n TabListProps,\n TabPanelProps,\n TabsContextValue,\n TabProps,\n} from './types';\nimport { useLocation, useNavigate, useHref } from 'react-router-dom';\nimport { TabsIndicators } from './TabsIndicators';\nimport {\n Tabs as AriaTabs,\n TabList as AriaTabList,\n Tab as AriaTab,\n TabPanel as AriaTabPanel,\n RouterProvider,\n TabProps as AriaTabProps,\n} from 'react-aria-components';\nimport { useStyles } from '../../hooks/useStyles';\nimport { TabsDefinition } from './definition';\nimport styles from './Tabs.module.css';\nimport clsx from 'clsx';\n\nconst TabsContext = createContext<TabsContextValue | undefined>(undefined);\n\nconst useTabsContext = () => {\n const context = useContext(TabsContext);\n if (!context) {\n throw new Error('Tab components must be used within a Tabs component');\n }\n return context;\n};\n\n/**\n * Utility function to determine if a tab should be active based on the matching strategy.\n * This follows the pattern used in WorkaroundNavLink from the sidebar.\n */\nconst isTabActive = (\n tabHref: string,\n currentPathname: string,\n matchStrategy: 'exact' | 'prefix',\n): boolean => {\n if (matchStrategy === 'exact') {\n return tabHref === currentPathname;\n }\n\n // Prefix matching - similar to WorkaroundNavLink behavior\n if (tabHref === currentPathname) {\n return true;\n }\n\n // Check if current path starts with tab href followed by a slash\n // This prevents /foo matching /foobar\n return currentPathname.startsWith(`${tabHref}/`);\n};\n\n/**\n * A component that renders a list of tabs.\n *\n * @public\n */\nexport const Tabs = (props: TabsProps) => {\n const { classNames, cleanedProps } = useStyles(TabsDefinition, props);\n const { className, children, ...rest } = cleanedProps;\n const tabsRef = useRef<HTMLDivElement>(null);\n const tabRefs = useRef<Map<string, HTMLDivElement>>(new Map());\n const [hoveredKey, setHoveredKey] = useState<string | null>(null);\n const prevHoveredKey = useRef<string | null>(null);\n let navigate = useNavigate();\n const location = useLocation();\n\n const setTabRef = (key: string, element: HTMLDivElement | null) => {\n if (element) {\n tabRefs.current.set(key, element);\n } else {\n tabRefs.current.delete(key);\n }\n };\n\n // If selectedKey is not provided, try to determine it from the current route\n const computedSelectedKey = (() => {\n const childrenArray = Children.toArray(children as ReactNode);\n for (const child of childrenArray) {\n if (isValidElement(child) && child.type === TabList) {\n const tabListChildren = Children.toArray(child.props.children);\n for (const tabChild of tabListChildren) {\n if (isValidElement(tabChild) && tabChild.props.href) {\n // Use tab-specific strategy, defaulting to 'exact'\n const strategy = tabChild.props.matchStrategy || 'exact';\n if (isTabActive(tabChild.props.href, location.pathname, strategy)) {\n return tabChild.props.id;\n }\n }\n }\n\n //No route matches - check if all tabs have hrefs (pure navigation)\n const allTabsHaveHref = tabListChildren.every(\n child => isValidElement(child) && child.props.href,\n );\n\n if (allTabsHaveHref) {\n // Pure navigation tabs, no route match\n return null;\n } else {\n // Mixed tabs or pure local state\n return undefined;\n }\n }\n }\n return undefined;\n })();\n\n if (!children) return null;\n\n const contextValue: TabsContextValue = {\n tabsRef,\n tabRefs,\n hoveredKey,\n prevHoveredKey,\n setHoveredKey,\n setTabRef,\n };\n\n return (\n <TabsContext.Provider value={contextValue}>\n <RouterProvider navigate={navigate} useHref={useHref}>\n <AriaTabs\n className={clsx(classNames.tabs, styles[classNames.tabs], className)}\n keyboardActivation=\"manual\"\n selectedKey={computedSelectedKey}\n ref={tabsRef}\n {...rest}\n >\n {children as ReactNode}\n </AriaTabs>\n </RouterProvider>\n </TabsContext.Provider>\n );\n};\n\n/**\n * A component that renders a list of tabs.\n *\n * @public\n */\nexport const TabList = (props: TabListProps) => {\n const { classNames, cleanedProps } = useStyles(TabsDefinition, props);\n const { className, children, ...rest } = cleanedProps;\n const { setHoveredKey, tabRefs, tabsRef, hoveredKey, prevHoveredKey } =\n useTabsContext();\n\n const handleHover = (key: string | null) => {\n setHoveredKey(key);\n };\n\n // Clone children with additional props for hover and ref management\n const enhancedChildren = Children.map(children as ReactNode, child => {\n if (isValidElement(child)) {\n return cloneElement(child, {\n onHoverStart: () => handleHover(child.props.id as string),\n onHoverEnd: () => handleHover(null),\n } as Partial<AriaTabProps>);\n }\n return child;\n });\n\n return (\n <div\n className={clsx(\n classNames.tabListWrapper,\n styles[classNames.tabListWrapper],\n className,\n )}\n >\n <AriaTabList\n className={clsx(classNames.tabList, styles[classNames.tabList])}\n aria-label=\"Toolbar tabs\"\n {...rest}\n >\n {enhancedChildren}\n </AriaTabList>\n <TabsIndicators\n tabRefs={tabRefs}\n tabsRef={tabsRef}\n hoveredKey={hoveredKey}\n prevHoveredKey={prevHoveredKey}\n />\n </div>\n );\n};\n\n/**\n * A component that renders a tab.\n *\n * @public\n */\nexport const Tab = (props: TabProps) => {\n const { classNames, cleanedProps } = useStyles(TabsDefinition, props);\n const {\n className,\n href,\n children,\n id,\n matchStrategy: _matchStrategy,\n ...rest\n } = cleanedProps;\n const { setTabRef } = useTabsContext();\n\n return (\n <AriaTab\n id={id}\n className={clsx(classNames.tab, styles[classNames.tab], className)}\n ref={el => setTabRef(id as string, el as HTMLDivElement)}\n href={href}\n {...rest}\n >\n {children}\n </AriaTab>\n );\n};\n\n/**\n * A component that renders the content of a tab.\n *\n * @public\n */\nexport const TabPanel = (props: TabPanelProps) => {\n const { classNames, cleanedProps } = useStyles(TabsDefinition, props);\n const { className, children, ...rest } = cleanedProps;\n\n return (\n <AriaTabPanel\n className={clsx(classNames.panel, styles[classNames.panel], className)}\n {...rest}\n >\n {children}\n </AriaTabPanel>\n );\n};\n"],"names":["child","AriaTabs","AriaTabList","AriaTab","AriaTabPanel"],"mappings":";;;;;;;;;;AAgDA,MAAM,WAAA,GAAc,cAA4C,MAAS,CAAA;AAEzE,MAAM,iBAAiB,MAAM;AAC3B,EAAA,MAAM,OAAA,GAAU,WAAW,WAAW,CAAA;AACtC,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,MAAM,qDAAqD,CAAA;AAAA,EACvE;AACA,EAAA,OAAO,OAAA;AACT,CAAA;AAMA,MA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AAW,QAAA,EAAU,GAAG,MAAK,GAAI,YAAA;AAEzC,EAAA,uBACE,GAAA;AAAA,IAACC,UAAA;AAAA,IAAA;AAAA,MACC,SAAA,EAAW,KAAK,UAAA,CAAW,KAAA,EAAO,OAAO,UAAA,CAAW,KAAK,GAAG,SAAS,CAAA;AAAA,MACpE,GAAG,IAAA;AAAA,MAEH;AAAA;AAAA,GACH;AAEJ;;;;"}
|
|
1
|
+
{"version":3,"file":"Tabs.esm.js","sources":["../../../src/components/Tabs/Tabs.tsx"],"sourcesContent":["/*\n * Copyright 2024 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 {\n useRef,\n useState,\n useMemo,\n Children,\n cloneElement,\n isValidElement,\n createContext,\n useContext,\n useEffect,\n useCallback,\n} from 'react';\nimport type { ReactNode } from 'react';\nimport type {\n TabsProps,\n TabListProps,\n TabPanelProps,\n TabsContextValue,\n TabProps,\n} from './types';\nimport { useLocation } from 'react-router-dom';\nimport { TabsIndicators } from './TabsIndicators';\nimport {\n Tabs as AriaTabs,\n TabList as AriaTabList,\n Tab as AriaTab,\n TabPanel as AriaTabPanel,\n TabProps as AriaTabProps,\n} from 'react-aria-components';\nimport { useStyles } from '../../hooks/useStyles';\nimport { TabsDefinition } from './definition';\nimport {\n isInternalLink,\n createRoutingRegistration,\n} from '../InternalLinkProvider';\nimport styles from './Tabs.module.css';\nimport clsx from 'clsx';\n\nconst { RoutingProvider, useRoutingRegistrationEffect } =\n createRoutingRegistration();\n\nconst TabsContext = createContext<TabsContextValue | undefined>(undefined);\n\nconst useTabsContext = () => {\n const context = useContext(TabsContext);\n if (!context) {\n throw new Error('Tab components must be used within a Tabs component');\n }\n return context;\n};\n\ntype TabSelectionContextValue = {\n registerRoutedTab: (id: string) => void;\n unregisterRoutedTab: (id: string) => void;\n registerActiveTab: (id: string, segmentCount: number) => void;\n unregisterActiveTab: (id: string) => void;\n};\n\nconst TabSelectionContext = createContext<TabSelectionContextValue | null>(\n null,\n);\n\n/**\n * Utility function to determine if a tab should be active based on the matching strategy.\n * This follows the pattern used in WorkaroundNavLink from the sidebar.\n */\nconst isTabActive = (\n tabHref: string,\n currentPathname: string,\n matchStrategy: 'exact' | 'prefix',\n): boolean => {\n if (matchStrategy === 'exact') {\n return tabHref === currentPathname;\n }\n\n // Prefix matching - similar to WorkaroundNavLink behavior\n if (tabHref === currentPathname) {\n return true;\n }\n\n // Check if current path starts with tab href followed by a slash\n // This prevents /foo matching /foobar\n return currentPathname.startsWith(`${tabHref}/`);\n};\n\n/**\n * A component that renders a list of tabs.\n *\n * @public\n */\nexport const Tabs = (props: TabsProps) => {\n const { classNames, cleanedProps } = useStyles(TabsDefinition, props);\n const { className, children, ...rest } = cleanedProps;\n const tabsRef = useRef<HTMLDivElement>(null);\n const tabRefs = useRef<Map<string, HTMLDivElement>>(new Map());\n const [hoveredKey, setHoveredKey] = useState<string | null>(null);\n const prevHoveredKey = useRef<string | null>(null);\n\n // State for tracking routed tabs (tabs with hrefs)\n const [routedTabs, setRoutedTabs] = useState<Set<string>>(() => new Set());\n\n // State for tracking active tabs reported by TabRouteRegistration components\n const [activeTabs, setActiveTabs] = useState<Map<string, number>>(\n () => new Map(),\n );\n\n const setTabRef = (key: string, element: HTMLDivElement | null) => {\n if (element) {\n tabRefs.current.set(key, element);\n } else {\n tabRefs.current.delete(key);\n }\n };\n\n // Compute the selected tab based on active tabs with highest segment count\n const selectedTabId = useMemo(() => {\n // No routed tabs - let React Aria handle selection (uncontrolled mode)\n if (routedTabs.size === 0) {\n return undefined;\n }\n\n // Has routed tabs but none are active - use empty string for no selection\n // (React Aria has a bug with null that causes infinite loops)\n if (activeTabs.size === 0) {\n return '';\n }\n\n let selectedId: string | null = null;\n let maxSegments = -1;\n\n activeTabs.forEach((segmentCount, id) => {\n // Pick tab with highest segment count, first one wins on tie\n if (segmentCount > maxSegments) {\n maxSegments = segmentCount;\n selectedId = id;\n }\n });\n\n return selectedId;\n }, [routedTabs, activeTabs]);\n\n const registerRoutedTab = useCallback((id: string) => {\n setRoutedTabs(prev => new Set(prev).add(id));\n }, []);\n\n const unregisterRoutedTab = useCallback((id: string) => {\n setRoutedTabs(prev => {\n const next = new Set(prev);\n next.delete(id);\n return next;\n });\n }, []);\n\n const registerActiveTab = useCallback((id: string, segmentCount: number) => {\n setActiveTabs(prev => new Map(prev).set(id, segmentCount));\n }, []);\n\n const unregisterActiveTab = useCallback((id: string) => {\n setActiveTabs(prev => {\n const next = new Map(prev);\n next.delete(id);\n return next;\n });\n }, []);\n\n if (!children) return null;\n\n const tabsContextValue: TabsContextValue = {\n tabsRef,\n tabRefs,\n hoveredKey,\n prevHoveredKey,\n setHoveredKey,\n setTabRef,\n };\n\n const selectionContextValue: TabSelectionContextValue = useMemo(\n () => ({\n registerRoutedTab,\n unregisterRoutedTab,\n registerActiveTab,\n unregisterActiveTab,\n }),\n [\n registerRoutedTab,\n unregisterRoutedTab,\n registerActiveTab,\n unregisterActiveTab,\n ],\n );\n\n return (\n <RoutingProvider>\n <TabsContext.Provider value={tabsContextValue}>\n <TabSelectionContext.Provider value={selectionContextValue}>\n <AriaTabs\n className={clsx(\n classNames.tabs,\n styles[classNames.tabs],\n className,\n )}\n keyboardActivation=\"manual\"\n selectedKey={selectedTabId}\n ref={tabsRef}\n {...rest}\n >\n {children as ReactNode}\n </AriaTabs>\n </TabSelectionContext.Provider>\n </TabsContext.Provider>\n </RoutingProvider>\n );\n};\n\n/**\n * A component that renders a list of tabs.\n *\n * @public\n */\nexport const TabList = (props: TabListProps) => {\n const { classNames, cleanedProps } = useStyles(TabsDefinition, props);\n const { className, children, ...rest } = cleanedProps;\n const { setHoveredKey, tabRefs, tabsRef, hoveredKey, prevHoveredKey } =\n useTabsContext();\n\n const handleHover = (key: string | null) => {\n setHoveredKey(key);\n };\n\n // Clone children with additional props for hover and ref management\n const enhancedChildren = Children.map(children as ReactNode, child => {\n if (isValidElement(child)) {\n return cloneElement(child, {\n onHoverStart: () => handleHover(child.props.id as string),\n onHoverEnd: () => handleHover(null),\n } as Partial<AriaTabProps>);\n }\n return child;\n });\n\n return (\n <div\n className={clsx(\n classNames.tabListWrapper,\n styles[classNames.tabListWrapper],\n className,\n )}\n >\n <AriaTabList\n className={clsx(classNames.tabList, styles[classNames.tabList])}\n aria-label=\"Toolbar tabs\"\n {...rest}\n >\n {enhancedChildren}\n </AriaTabList>\n <TabsIndicators\n tabRefs={tabRefs}\n tabsRef={tabsRef}\n hoveredKey={hoveredKey}\n prevHoveredKey={prevHoveredKey}\n />\n </div>\n );\n};\n\n/**\n * Internal component for tabs with internal hrefs.\n * Handles routing registration and active tab tracking.\n * Separated to avoid conditional hook usage in Tab component.\n * @internal\n */\nfunction RoutedTabEffects({\n id,\n href,\n matchStrategy = 'exact',\n}: {\n id: string;\n href: string;\n matchStrategy?: 'exact' | 'prefix';\n}) {\n const selectionCtx = useContext(TabSelectionContext);\n const location = useLocation();\n\n // Register with RoutingProvider for conditional RouterProvider wrapping\n useRoutingRegistrationEffect(href);\n\n // Register as a routed tab (for controlled vs uncontrolled mode)\n useEffect(() => {\n if (selectionCtx) {\n selectionCtx.registerRoutedTab(id);\n return () => selectionCtx.unregisterRoutedTab(id);\n }\n return undefined;\n }, [id, selectionCtx]);\n\n // Register as active tab when URL matches (for tab selection)\n const isActive = isTabActive(href, location.pathname, matchStrategy);\n const segmentCount = href.split('/').filter(Boolean).length;\n\n useEffect(() => {\n if (isActive && selectionCtx) {\n selectionCtx.registerActiveTab(id, segmentCount);\n return () => selectionCtx.unregisterActiveTab(id);\n }\n return undefined;\n }, [isActive, id, segmentCount, selectionCtx]);\n\n return null;\n}\n\n/**\n * A component that renders a tab.\n *\n * @public\n */\nexport const Tab = (props: TabProps) => {\n const { classNames, cleanedProps } = useStyles(TabsDefinition, props);\n const { className, href, children, id, matchStrategy, ...rest } =\n cleanedProps;\n const { setTabRef } = useTabsContext();\n\n return (\n <>\n {isInternalLink(href) && (\n <RoutedTabEffects\n id={id as string}\n href={href}\n matchStrategy={matchStrategy}\n />\n )}\n <AriaTab\n id={id}\n className={clsx(classNames.tab, styles[classNames.tab], className)}\n ref={el => setTabRef(id as string, el as HTMLDivElement)}\n href={href}\n {...rest}\n >\n {children}\n </AriaTab>\n </>\n );\n};\n\n/**\n * A component that renders the content of a tab.\n *\n * @public\n */\nexport const TabPanel = (props: TabPanelProps) => {\n const { classNames, cleanedProps } = useStyles(TabsDefinition, props);\n const { className, children, ...rest } = cleanedProps;\n\n return (\n <AriaTabPanel\n className={clsx(classNames.panel, styles[classNames.panel], className)}\n {...rest}\n >\n {children}\n </AriaTabPanel>\n );\n};\n"],"names":["AriaTabs","AriaTabList","AriaTab","AriaTabPanel"],"mappings":";;;;;;;;;;;AAsDA,MAAM,EAAE,eAAA,EAAiB,4BAAA,EAA6B,GACpD,yBAAA,EAA0B;AAE5B,MAAM,WAAA,GAAc,cAA4C,MAAS,CAAA;AAEzE,MAAM,iBAAiB,MAAM;AAC3B,EAAA,MAAM,OAAA,GAAU,WAAW,WAAW,CAAA;AACtC,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,MAAM,qDAAqD,CAAA;AAAA,EACvE;AACA,EAAA,OAAO,OAAA;AACT,CAAA;AASA,MAAM,mBAAA,GAAsB,aAAA;AAAA,EAC1B;AACF,CAAA;AAMA,MAAM,WAAA,GAAc,CAClB,OAAA,EACA,eAAA,EACA,aAAA,KACY;AACZ,EAAA,IAAI,kBAAkB,OAAA,EAAS;AAC7B,IAAA,OAAO,OAAA,KAAY,eAAA;AAAA,EACrB;AAGA,EAAA,IAAI,YAAY,eAAA,EAAiB;AAC/B,IAA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AM,EAAE,SAAA,EAAU,GAAI,cAAA,EAAe;AAErC,EAAA,uBACE,IAAA,CAAA,QAAA,EAAA,EACG,QAAA,EAAA;AAAA,IAAA,cAAA,CAAe,IAAI,CAAA,oBAClB,GAAA;AAAA,MAAC,gBAAA;AAAA,MAAA;AAAA,QACC,EAAA;AAAA,QACA,IAAA;AAAA,QACA;AAAA;AAAA,KACF;AAAA,oBAEF,GAAA;AAAA,MAACC,KAAA;AAAA,MAAA;AAAA,QACC,EAAA;AAAA,QACA,SAAA,EAAW,KAAK,UAAA,CAAW,GAAA,EAAK,OAAO,UAAA,CAAW,GAAG,GAAG,SAAS,CAAA;AAAA,QACjE,GAAA,EAAK,CAAA,EAAA,KAAM,SAAA,CAAU,EAAA,EAAc,EAAoB,CAAA;AAAA,QACvD,IAAA;AAAA,QACC,GAAG,IAAA;AAAA,QAEH;AAAA;AAAA;AACH,GAAA,EACF,CAAA;AAEJ;AAOO,MAAM,QAAA,GAAW,CAAC,KAAA,KAAyB;AAChD,EAAA,MAAM,EAAE,UAAA,EAAY,YAAA,EAAa,GAAI,SAAA,CAAU,gBAAgB,KAAK,CAAA;AACpE,EAAA,MAAM,EAAE,SAAA,EAAW,QAAA,EAAU,GAAG,MAAK,GAAI,YAAA;AAEzC,EAAA,uBACE,GAAA;AAAA,IAACC,UAAA;AAAA,IAAA;AAAA,MACC,SAAA,EAAW,KAAK,UAAA,CAAW,KAAA,EAAO,OAAO,UAAA,CAAW,KAAK,GAAG,SAAS,CAAA;AAAA,MACpE,GAAG,IAAA;AAAA,MAEH;AAAA;AAAA,GACH;AAEJ;;;;"}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
|
-
import { TagGroup as TagGroup$1, TagList, Tag as Tag$1, Button
|
|
2
|
+
import { TagGroup as TagGroup$1, TagList, Tag as Tag$1, Button } from 'react-aria-components';
|
|
3
3
|
import { RiCloseCircleLine } from '@remixicon/react';
|
|
4
4
|
import clsx from 'clsx';
|
|
5
5
|
import { useStyles } from '../../hooks/useStyles.esm.js';
|
|
6
6
|
import { TagGroupDefinition } from './definition.esm.js';
|
|
7
|
-
import {
|
|
8
|
-
import { useNavigate, useHref } from 'react-router-dom';
|
|
7
|
+
import { createRoutingRegistration } from '../InternalLinkProvider/InternalLinkProvider.esm.js';
|
|
9
8
|
import styles from './TagGroup.module.css.esm.js';
|
|
10
9
|
|
|
10
|
+
const { RoutingProvider, useRoutingRegistrationEffect } = createRoutingRegistration();
|
|
11
11
|
const TagGroup = (props) => {
|
|
12
12
|
const { classNames, cleanedProps } = useStyles(TagGroupDefinition, props);
|
|
13
13
|
const { items, children, renderEmptyState, ...rest } = cleanedProps;
|
|
14
|
-
return /* @__PURE__ */ jsx(
|
|
14
|
+
return /* @__PURE__ */ jsx(RoutingProvider, { children: /* @__PURE__ */ jsx(
|
|
15
15
|
TagGroup$1,
|
|
16
16
|
{
|
|
17
17
|
className: clsx(classNames.group, styles[classNames.group]),
|
|
@@ -26,7 +26,7 @@ const TagGroup = (props) => {
|
|
|
26
26
|
}
|
|
27
27
|
)
|
|
28
28
|
}
|
|
29
|
-
);
|
|
29
|
+
) });
|
|
30
30
|
};
|
|
31
31
|
const Tag = (props) => {
|
|
32
32
|
const { classNames, cleanedProps } = useStyles(TagGroupDefinition, {
|
|
@@ -35,10 +35,8 @@ const Tag = (props) => {
|
|
|
35
35
|
});
|
|
36
36
|
const { children, className, icon, size, href, ...rest } = cleanedProps;
|
|
37
37
|
const textValue = typeof children === "string" ? children : void 0;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const isExternal = isExternalLink(href);
|
|
41
|
-
const content = /* @__PURE__ */ jsx(
|
|
38
|
+
useRoutingRegistrationEffect(href);
|
|
39
|
+
return /* @__PURE__ */ jsx(
|
|
42
40
|
Tag$1,
|
|
43
41
|
{
|
|
44
42
|
textValue,
|
|
@@ -69,10 +67,6 @@ const Tag = (props) => {
|
|
|
69
67
|
] })
|
|
70
68
|
}
|
|
71
69
|
);
|
|
72
|
-
if (isLink && !isExternal) {
|
|
73
|
-
return /* @__PURE__ */ jsx(RouterProvider, { navigate, useHref, children: content });
|
|
74
|
-
}
|
|
75
|
-
return content;
|
|
76
70
|
};
|
|
77
71
|
|
|
78
72
|
export { Tag, TagGroup };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TagGroup.esm.js","sources":["../../../src/components/TagGroup/TagGroup.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 type { TagProps, TagGroupProps } from './types';\nimport {\n TagGroup as ReactAriaTagGroup,\n TagList as ReactAriaTagList,\n Tag as ReactAriaTag,\n Button as ReactAriaButton,\n
|
|
1
|
+
{"version":3,"file":"TagGroup.esm.js","sources":["../../../src/components/TagGroup/TagGroup.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 type { TagProps, TagGroupProps } from './types';\nimport {\n TagGroup as ReactAriaTagGroup,\n TagList as ReactAriaTagList,\n Tag as ReactAriaTag,\n Button as ReactAriaButton,\n} from 'react-aria-components';\nimport type { ReactNode } from 'react';\nimport { RiCloseCircleLine } from '@remixicon/react';\nimport clsx from 'clsx';\nimport { useStyles } from '../../hooks/useStyles';\nimport { TagGroupDefinition } from './definition';\nimport { createRoutingRegistration } from '../InternalLinkProvider';\nimport styles from './TagGroup.module.css';\n\nconst { RoutingProvider, useRoutingRegistrationEffect } =\n createRoutingRegistration();\n\n/**\n * A component that renders a list of tags.\n *\n * @public\n */\nexport const TagGroup = <T extends object>(props: TagGroupProps<T>) => {\n const { classNames, cleanedProps } = useStyles(TagGroupDefinition, props);\n const { items, children, renderEmptyState, ...rest } = cleanedProps;\n\n return (\n <RoutingProvider>\n <ReactAriaTagGroup\n className={clsx(classNames.group, styles[classNames.group])}\n {...rest}\n >\n <ReactAriaTagList\n className={clsx(classNames.list, styles[classNames.list])}\n items={items}\n renderEmptyState={renderEmptyState}\n >\n {children}\n </ReactAriaTagList>\n </ReactAriaTagGroup>\n </RoutingProvider>\n );\n};\n\n/**\n * A component that renders a tag.\n *\n * @public\n */\nexport const Tag = (props: TagProps) => {\n const { classNames, cleanedProps } = useStyles(TagGroupDefinition, {\n size: 'small',\n ...props,\n });\n const { children, className, icon, size, href, ...rest } = cleanedProps;\n const textValue = typeof children === 'string' ? children : undefined;\n\n useRoutingRegistrationEffect(href);\n\n return (\n <ReactAriaTag\n textValue={textValue}\n className={clsx(classNames.tag, styles[classNames.tag], className)}\n data-size={size}\n href={href}\n {...rest}\n >\n {({ allowsRemoving }) => (\n <>\n {icon && (\n <span\n className={clsx(classNames.tagIcon, styles[classNames.tagIcon])}\n >\n {icon}\n </span>\n )}\n {children as ReactNode}\n {allowsRemoving && (\n <ReactAriaButton\n className={clsx(\n classNames.tagRemoveButton,\n styles[classNames.tagRemoveButton],\n )}\n slot=\"remove\"\n >\n <RiCloseCircleLine size={16} />\n </ReactAriaButton>\n )}\n </>\n )}\n </ReactAriaTag>\n );\n};\n"],"names":["ReactAriaTagGroup","ReactAriaTagList","ReactAriaTag","ReactAriaButton"],"mappings":";;;;;;;;;AA+BA,MAAM,EAAE,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,OAAO,UAAA,CAAW,GAAG,GAAG,SAAS,CAAA;AAAA,MACjE,WAAA,EAAW,IAAA;AAAA,MACX,IAAA;AAAA,MACC,GAAG,IAAA;AAAA,MAEH,QAAA,EAAA,CAAC,EAAE,cAAA,EAAe,qBACjB,IAAA,CAAA,QAAA,EAAA,EACG,QAAA,EAAA;AAAA,QAAA,IAAA,oBACC,GAAA;AAAA,UAAC,MAAA;AAAA,UAAA;AAAA,YACC,WAAW,IAAA,CAAK,UAAA,CAAW,SAAS,MAAA,CAAO,UAAA,CAAW,OAAO,CAAC,CAAA;AAAA,YAE7D,QAAA,EAAA;AAAA;AAAA,SACH;AAAA,QAED,QAAA;AAAA,QACA,cAAA,oBACC,GAAA;AAAA,UAACC,MAAA;AAAA,UAAA;AAAA,YACC,SAAA,EAAW,IAAA;AAAA,cACT,UAAA,CAAW,eAAA;AAAA,cACX,MAAA,CAAO,WAAW,eAAe;AAAA,aACnC;AAAA,YACA,IAAA,EAAK,QAAA;AAAA,YAEL,QAAA,kBAAA,GAAA,CAAC,iBAAA,EAAA,EAAkB,IAAA,EAAM,EAAA,EAAI;AAAA;AAAA;AAC/B,OAAA,EAEJ;AAAA;AAAA,GAEJ;AAEJ;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineComponent.esm.js","sources":["../../../src/hooks/useDefinition/defineComponent.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 */\n\nimport type { ComponentConfig, SurfacePropsConstraint } from './types';\n\nexport function defineComponent<P extends Record<string, any>>() {\n return <\n const S extends Record<string, string>,\n const C extends ComponentConfig<P, S>,\n >(\n config: C & SurfacePropsConstraint<P, C['surface']>,\n ): C => config;\n}\n"],"names":[],"mappings":"AAkBO,SAAS,eAAA,GAAiD;AAC/D,EAAA,OAAO,CAIL,MAAA,KACM,MAAA;AACV;;;;"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { breakpoints } from '../useBreakpoint.esm.js';
|
|
2
|
+
import { utilityClassMap } from '../../utils/utilityClassMap.esm.js';
|
|
3
|
+
|
|
4
|
+
const namedBreakpoints = breakpoints.filter((b) => b.id !== "initial");
|
|
5
|
+
function isResponsiveObject(value) {
|
|
6
|
+
return typeof value === "object" && value !== null && namedBreakpoints.some((b) => b.id in value);
|
|
7
|
+
}
|
|
8
|
+
function resolveResponsiveValue(value, breakpoint) {
|
|
9
|
+
if (!isResponsiveObject(value)) {
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
const index = breakpoints.findIndex((b) => b.id === breakpoint);
|
|
13
|
+
for (let i = index; i >= 0; i--) {
|
|
14
|
+
const key = breakpoints[i].id;
|
|
15
|
+
if (key in value && value[key] !== void 0) {
|
|
16
|
+
return value[key];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
for (let i = 0; i < breakpoints.length; i++) {
|
|
20
|
+
const key = breakpoints[i].id;
|
|
21
|
+
if (key in value && value[key] !== void 0) {
|
|
22
|
+
return value[key];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
function processUtilityProps(props, utilityPropKeys) {
|
|
28
|
+
const utilityClassList = [];
|
|
29
|
+
const generatedStyle = {};
|
|
30
|
+
const handleUtilityValue = (key, val, prefix = "") => {
|
|
31
|
+
const utilityConfig = utilityClassMap[key];
|
|
32
|
+
if (!utilityConfig) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const values = utilityConfig.values;
|
|
36
|
+
if (values.length > 0 && values.includes(val)) {
|
|
37
|
+
const className = prefix ? `${prefix}${utilityConfig.class}-${val}` : `${utilityConfig.class}-${val}`;
|
|
38
|
+
utilityClassList.push(className);
|
|
39
|
+
} else if ("cssVar" in utilityConfig && utilityConfig.cssVar) {
|
|
40
|
+
const cssVar = utilityConfig.cssVar;
|
|
41
|
+
const cssVarKey = prefix ? `${cssVar}-${prefix.slice(0, -1)}` : cssVar;
|
|
42
|
+
generatedStyle[cssVarKey] = val;
|
|
43
|
+
const className = prefix ? `${prefix}${utilityConfig.class}` : utilityConfig.class;
|
|
44
|
+
utilityClassList.push(className);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
for (const key of utilityPropKeys) {
|
|
48
|
+
const value = props[key];
|
|
49
|
+
if (value === void 0 || value === null) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (typeof value === "object" && value !== null) {
|
|
53
|
+
const breakpointValues = value;
|
|
54
|
+
for (const bp in breakpointValues) {
|
|
55
|
+
const prefix = bp === "initial" ? "" : `${bp}:`;
|
|
56
|
+
handleUtilityValue(key, breakpointValues[bp], prefix);
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
handleUtilityValue(key, value);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
utilityClasses: utilityClassList.join(" "),
|
|
64
|
+
utilityStyle: generatedStyle
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { processUtilityProps, resolveResponsiveValue };
|
|
69
|
+
//# sourceMappingURL=helpers.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.esm.js","sources":["../../../src/hooks/useDefinition/helpers.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 */\n\nimport { breakpoints } from '../useBreakpoint';\nimport { utilityClassMap } from '../../utils/utilityClassMap';\nimport type { UnwrapResponsive, UtilityStyle } from './types';\n\nconst namedBreakpoints = breakpoints.filter(b => b.id !== 'initial');\n\nfunction isResponsiveObject(value: unknown): value is Record<string, unknown> {\n return (\n typeof value === 'object' &&\n value !== null &&\n namedBreakpoints.some(b => b.id in value)\n );\n}\n\nexport function resolveResponsiveValue<T>(\n value: T,\n breakpoint: string,\n): UnwrapResponsive<T> {\n if (!isResponsiveObject(value)) {\n return value as UnwrapResponsive<T>;\n }\n\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 const key = breakpoints[i].id;\n if (key in value && value[key] !== undefined) {\n return value[key] as UnwrapResponsive<T>;\n }\n }\n\n // If no value found, check from smallest breakpoint up\n for (let i = 0; i < breakpoints.length; i++) {\n const key = breakpoints[i].id;\n if (key in value && value[key] !== undefined) {\n return value[key] as UnwrapResponsive<T>;\n }\n }\n\n return value as UnwrapResponsive<T>;\n}\n\nexport function processUtilityProps<Keys extends string>(\n props: Record<string, any>,\n utilityPropKeys: readonly Keys[],\n): { utilityClasses: string; utilityStyle: UtilityStyle<Keys> } {\n const utilityClassList: string[] = [];\n const generatedStyle: Record<string, unknown> = {};\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 const values = utilityConfig.values as readonly (string | number)[];\n if (values.length > 0 && values.includes(val as string | number)) {\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 ('cssVar' in utilityConfig && 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 cssVar = utilityConfig.cssVar;\n const cssVarKey = prefix ? `${cssVar}-${prefix.slice(0, -1)}` : cssVar;\n // CSS custom properties need to be set on the style object\n generatedStyle[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 utilityPropKeys) {\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 return {\n utilityClasses: utilityClassList.join(' '),\n utilityStyle: generatedStyle as UtilityStyle<Keys>,\n };\n}\n"],"names":[],"mappings":";;;AAoBA,MAAM,mBAAmB,WAAA,CAAY,MAAA,CAAO,CAAA,CAAA,KAAK,CAAA,CAAE,OAAO,SAAS,CAAA;AAEnE,SAAS,mBAAmB,KAAA,EAAkD;AAC5E,EAAA,OACE,OAAO,KAAA,KAAU,QAAA,IACjB,KAAA,KAAU,IAAA,IACV,iBAAiB,IAAA,CAAK,CAAA,CAAA,KAAK,CAAA,CAAE,EAAA,IAAM,KAAK,CAAA;AAE5C;AAEO,SAAS,sBAAA,CACd,OACA,UAAA,EACqB;AACrB,EAAA,IAAI,CAAC,kBAAA,CAAmB,KAAK,CAAA,EAAG;AAC9B,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,MAAM,QAAQ,WAAA,CAAY,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AA,EACF;AAEA,EAAA,OAAO;AAAA,IACL,cAAA,EAAgB,gBAAA,CAAiB,IAAA,CAAK,GAAG,CAAA;AAAA,IACzC,YAAA,EAAc;AAAA,GAChB;AACF;;;;"}
|
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
{
|
|
53
|
+
[utilityClasses]: utilityTarget === name,
|
|
54
|
+
[ownPropsResolved.className]: classNameTarget === name
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
let children;
|
|
59
|
+
let surfaceChildren;
|
|
60
|
+
if (definition.surface === "container") {
|
|
61
|
+
surfaceChildren = resolvedSurface ? /* @__PURE__ */ jsx(SurfaceProvider, { surface: resolvedSurface, children: props.children }) : props.children;
|
|
62
|
+
} else {
|
|
63
|
+
children = props.children;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
ownProps: {
|
|
67
|
+
classes,
|
|
68
|
+
...ownPropsResolved,
|
|
69
|
+
...definition.surface === "container" ? { surfaceChildren } : { children }
|
|
70
|
+
},
|
|
71
|
+
restProps,
|
|
72
|
+
dataAttributes,
|
|
73
|
+
utilityStyle
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { useDefinition };
|
|
78
|
+
//# 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 {\n [utilityClasses]: utilityTarget === name,\n [ownPropsResolved.className]: classNameTarget === name,\n },\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;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;AAEJ,EAAA,IAAI,UAAA,CAAW,YAAY,WAAA,EAAa;AACtC,IAAA,eAAA,GAAkB,eAAA,uBACf,eAAA,EAAA,EAAgB,OAAA,EAAS,iBACvB,QAAA,EAAA,KAAA,CAAM,QAAA,EACT,IAEA,KAAA,CAAM,QAAA;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,SAA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,CAAA,EAAG,EAAS,CAAA;AAGZ,EAAA,MAAM,WAAA,GAAc;AAAA,IAClB,GAAG,cAAA;AAAA,IACH,GAAG;AAAA,GACL;AAGA,EAAA,MAAM,YAAA,GAAe;AAAA,IACnB,GAAG,gBAAA;AAAA,IACH,KAAA,EAAO;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,IAA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