storybook-addon-css-properties
Version:
Edit CSS properties defined in your component to see how stories react
1 lines • 30.4 kB
Source Map (JSON)
{"version":3,"sources":["../src/constants.ts","../src/withRoundTrip.ts","../src/preview.ts"],"names":["ADDON_ID","KEY","EVENTS","isCssProperty","key","flattenParameters","config","flattened","value","cssKey","cssValue","storyStyles","currentContext","observer","previousStoryId","previousStoryContext","withRoundTrip","storyFn","context","storyId","cleanupPreviousStoryStyles","cleanupObserver","storyResult","applyInitialValues","useChannel","styles","updatedStyles","applyStylesToStory","setupMutationObserver","setupDocsModeMutationObserver","mutations","hasRelevantChanges","mutation","node","element","cssVarsConfig","flattenedParams","initialStyles","currentStoryStyles","isDocsMode","targetSelector","elements","storyContainers","container","el","computedStyle","cssVar","style","targetElements","smartElement","findComponentInContainer","findComponentInStoryRoot","property","root","previousContext","previousStyles","elementWithCssVars","findElementUsingCssVariables","deepestElement","findDeepestMeaningfulElement","i","maxDepth","walker","hasClass","hasId","depth","parent","preview","preview_default"],"mappings":"6DAAO,IAAMA,CAAW,CAAA,gCAAA,CAEXC,CAAM,CAAA,SAAA,CAENC,CAAS,CAAA,CAEpB,OAAA,CAAS,CAAGF,EAAAA,CAAQ,CACtB,QAAA,CAAA,ECDA,SAASG,CAAAA,CAAcC,CAAiC,CAAA,CACtD,OAAOA,CAAAA,CAAI,WAAW,IAAI,CAC5B,CAGA,SAASC,CAAkBC,CAAAA,CAAAA,CAAuD,CAChF,IAAMC,CAA+C,CAAA,EAErD,CAAA,OAAA,MAAA,CAAO,OAAQD,CAAAA,CAAM,CAAE,CAAA,OAAA,CAAQ,CAAC,CAACF,CAAAA,CAAKI,CAAK,CAAA,GAAM,CAC3CL,CAAAA,CAAcC,CAAG,CAAA,CAEnBG,CAAUH,CAAAA,CAAG,CAAII,CAAAA,CAAAA,CAIjB,MAAO,CAAA,OAAA,CADUA,CACM,CAAA,CAAE,QAAQ,CAAC,CAACC,CAAQC,CAAAA,CAAQ,CAAM,GAAA,CACnDP,CAAcM,CAAAA,C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ID = `${ADDON_ID}/panel`;\r\nexport const KEY = `cssVars`;\r\n\r\nexport const EVENTS = {\r\n RESULT: `${ADDON_ID}/result`,\r\n REQUEST: `${ADDON_ID}/request`,\r\n};\r\n","import type { Renderer, DecoratorFunction } from \"storybook/internal/types\";\r\nimport { useChannel } from \"storybook/preview-api\";\r\nimport { EVENTS } from \"./constants\";\r\nimport type { Parameters, CssPropertyConfig, CssProperty } from \"./types\";\r\n\r\n// Helper function to check if a key is a CSS property (starts with --)\r\nfunction isCssProperty(key: string): key is CssProperty {\r\n return key.startsWith('--');\r\n}\r\n\r\n// Helper function to flatten categorized parameters into a flat structure for processing\r\nfunction flattenParameters(config: Parameters): Record<string, CssPropertyConfig> {\r\n const flattened: Record<string, CssPropertyConfig> = {};\r\n \r\n Object.entries(config).forEach(([key, value]) => {\r\n if (isCssProperty(key)) {\r\n // Direct CSS property\r\n flattened[key] = value as CssPropertyConfig;\r\n } else {\r\n // Category - extract all CSS properties from it\r\n const category = value as Record<string, CssPropertyConfig>;\r\n Object.entries(category).forEach(([cssKey, cssValue]) => {\r\n if (isCssProperty(cssKey)) {\r\n flattened[cssKey] = cssValue;\r\n }\r\n });\r\n }\r\n });\r\n \r\n return flattened;\r\n}\r\n\r\n// Store styles per story instance\r\nlet storyStyles: Map<string, Record<string, string>> = new Map();\r\nlet currentContext: any = null;\r\nlet observer: MutationObserver | null = null;\r\nlet previousStoryId: string | null = null;\r\nlet previousStoryContext: any = null;\r\n\r\nexport const withRoundTrip: DecoratorFunction<Renderer> = (storyFn, context) => {\r\n const storyId = context.id;\r\n \r\n // Clean up previous story's CSS variables if switching stories\r\n if (previousStoryId && previousStoryId !== storyId && previousStoryContext) {\r\n cleanupPreviousStoryStyles(previousStoryId, previousStoryContext);\r\n }\r\n \r\n // Update tracking variables\r\n previousStoryId = storyId;\r\n previousStoryContext = currentContext; // Store the old context before updating\r\n currentContext = context;\r\n \r\n // Clean up previous observer when switching stories\r\n cleanupObserver();\r\n \r\n // Get the story function result\r\n const storyResult = storyFn();\r\n \r\n // Only set up CSS vars if this specific story has them configured\r\n const storyHasCssVars = context?.parameters?.cssVars && \r\n Object.keys(context.parameters.cssVars).length > 0;\r\n \r\n if (storyHasCssVars) {\r\n // Initialize styles for this story\r\n storyStyles.set(storyId, {});\r\n \r\n // Apply initial values from story parameters\r\n applyInitialValues(storyId);\r\n \r\n useChannel({\r\n [EVENTS.REQUEST]: (styles: Record<string, string>) => {\r\n // Store styles for this specific story\r\n const currentStoryStyles = storyStyles.get(storyId) || {};\r\n const updatedStyles = { ...currentStoryStyles, ...styles };\r\n storyStyles.set(storyId, updatedStyles);\r\n \r\n // Apply styles only to this story's elements\r\n applyStylesToStory(storyId, updatedStyles);\r\n \r\n // Set up observer to watch for DOM changes if not already set up\r\n if (!observer) {\r\n setupMutationObserver();\r\n }\r\n }\r\n });\r\n }\r\n\r\n return storyResult;\r\n};\r\n\r\nfunction setupDocsModeMutationObserver() {\r\n // For docs mode, observe the entire document for changes\r\n const docsObserver = new MutationObserver((mutations) => {\r\n let hasRelevantChanges = false;\r\n \r\n mutations.forEach((mutation) => {\r\n if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {\r\n // Check if any added nodes contain story components\r\n for (const node of mutation.addedNodes) {\r\n if (node.nodeType === Node.ELEMENT_NODE) {\r\n const element = node as Element;\r\n // Check if the added node is or contains story content\r\n if (element.matches?.('[data-docs-story], .sb-story') || \r\n element.querySelector?.('[data-docs-story], .sb-story')) {\r\n hasRelevantChanges = true;\r\n break;\r\n }\r\n }\r\n }\r\n }\r\n });\r\n \r\n if (hasRelevantChanges) {\r\n // Reapply styles for all stories that have them\r\n storyStyles.forEach((styles, storyId) => {\r\n applyStylesToStory(storyId, styles);\r\n });\r\n }\r\n });\r\n\r\n docsObserver.observe(document.body, {\r\n childList: true,\r\n subtree: true\r\n });\r\n}\r\n\r\nfunction applyInitialValues(storyId: string): void {\r\n // Get CSS vars configuration from story parameters\r\n const cssVarsConfig = currentContext?.parameters?.cssVars;\r\n if (!cssVarsConfig) {\r\n return;\r\n }\r\n \r\n // Flatten the categorized parameters to get all CSS properties\r\n const flattenedParams = flattenParameters(cssVarsConfig as Parameters);\r\n \r\n // Extract values that are explicitly set, initialize others as empty\r\n const initialStyles: Record<string, string> = {};\r\n Object.entries(flattenedParams).forEach(([key, config]) => {\r\n // Initialize with explicit value or empty string\r\n initialStyles[key] = config.value || '';\r\n });\r\n \r\n // Store initial styles for this story (even if all are empty)\r\n const currentStoryStyles = storyStyles.get(storyId) || {};\r\n storyStyles.set(storyId, { ...currentStoryStyles, ...initialStyles });\r\n \r\n // Apply styles immediately to this story (this will remove properties for empty values)\r\n applyStylesToStory(storyId, storyStyles.get(storyId)!);\r\n \r\n // For docs mode, also retry after delays since MDX components render asynchronously\r\n if (currentContext?.viewMode === 'docs') {\r\n setTimeout(() => applyStylesToStory(storyId, storyStyles.get(storyId)!), 100);\r\n setTimeout(() => applyStylesToStory(storyId, storyStyles.get(storyId)!), 300);\r\n }\r\n \r\n // Set up observers for DOM changes\r\n if (!observer) {\r\n setupMutationObserver();\r\n }\r\n if (currentContext?.viewMode === 'docs') {\r\n setupDocsModeMutationObserver();\r\n }\r\n}\r\n\r\nfunction applyStylesToStory(storyId: string, styles: Record<string, string>): void {\r\n const isDocsMode = currentContext?.viewMode === 'docs';\r\n const targetSelector = currentContext?.parameters?.cssVarsTarget;\r\n \r\n // Only proceed if we actually have styles configuration (even if values are empty)\r\n if (Object.keys(styles).length === 0) {\r\n return;\r\n }\r\n \r\n let elements: HTMLElement[] = [];\r\n \r\n if (isDocsMode) {\r\n // In docs mode, find story containers that match this specific story\r\n // Look for containers that might contain this story's components\r\n if (targetSelector) {\r\n // User specified a custom target\r\n const storyContainers = document.querySelectorAll(`[data-docs-story=\"${storyId}\"], [id=\"story--${storyId}\"]`);\r\n if (storyContainers.length === 0) {\r\n // If we can't find specific containers, be very conservative\r\n // Only apply to elements that actually need these specific CSS variables\r\n const allContainers = document.querySelectorAll('[data-docs-story], .sb-story');\r\n allContainers.forEach(container => {\r\n const targetElements = container.querySelectorAll<HTMLElement>(targetSelector);\r\n targetElements.forEach(el => {\r\n // Only add elements that already use the specific CSS variables we're setting\r\n const computedStyle = window.getComputedStyle(el);\r\n const usesOurVars = Object.keys(styles).some(cssVar => \r\n computedStyle.getPropertyValue(cssVar) || \r\n ['background', 'backgroundColor', 'color'].some(style => \r\n computedStyle.getPropertyValue(style).includes(`var(${cssVar})`))\r\n );\r\n if (usesOurVars) {\r\n elements.push(el);\r\n }\r\n });\r\n });\r\n } else {\r\n storyContainers.forEach(container => {\r\n const targetElements = container.querySelectorAll<HTMLElement>(targetSelector);\r\n elements.push(...Array.from(targetElements));\r\n });\r\n }\r\n } else {\r\n // Use smart detection\r\n const storyContainers = document.querySelectorAll(`[data-docs-story=\"${storyId}\"], [id=\"story--${storyId}\"]`);\r\n if (storyContainers.length === 0) {\r\n // If we can't find specific containers, be very conservative\r\n // Only apply to elements that actually need these specific CSS variables\r\n const allContainers = document.querySelectorAll('[data-docs-story], .sb-story');\r\n allContainers.forEach(container => {\r\n const smartElement = findComponentInContainer(container as HTMLElement);\r\n if (smartElement) {\r\n const computedStyle = window.getComputedStyle(smartElement);\r\n // Only add elements that use the specific CSS variables we're setting\r\n const usesOurVars = Object.keys(styles).some(cssVar => \r\n computedStyle.getPropertyValue(cssVar) || \r\n ['background', 'backgroundColor', 'color'].some(style => \r\n computedStyle.getPropertyValue(style).includes(`var(${cssVar})`))\r\n );\r\n if (usesOurVars) {\r\n elements.push(smartElement);\r\n }\r\n }\r\n });\r\n } else {\r\n storyContainers.forEach(container => {\r\n const smartElement = findComponentInContainer(container as HTMLElement);\r\n if (smartElement) elements.push(smartElement);\r\n });\r\n }\r\n }\r\n } else {\r\n // For regular story mode\r\n if (targetSelector) {\r\n const element = document.querySelector<HTMLElement>(`#storybook-root ${targetSelector}`);\r\n if (element) elements = [element];\r\n } else {\r\n // Use smart component detection\r\n const smartElement = findComponentInStoryRoot();\r\n if (smartElement) elements = [smartElement];\r\n }\r\n }\r\n \r\n // Apply styles to all found elements immediately\r\n elements.forEach(element => {\r\n Object.entries(styles).forEach(([property, value]) => {\r\n if (value) {\r\n element.style.setProperty(property, value, isDocsMode ? 'important' : '');\r\n } else {\r\n element.style.removeProperty(property);\r\n }\r\n });\r\n });\r\n}\r\n\r\nfunction setupMutationObserver() {\r\n const root = document.getElementById('storybook-root');\r\n if (!root) return;\r\n\r\n observer = new MutationObserver((mutations) => {\r\n let hasRelevantChanges = false;\r\n \r\n mutations.forEach((mutation) => {\r\n if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {\r\n // Check if any added nodes are elements (not just text nodes)\r\n for (const node of mutation.addedNodes) {\r\n if (node.nodeType === Node.ELEMENT_NODE) {\r\n hasRelevantChanges = true;\r\n break;\r\n }\r\n }\r\n }\r\n });\r\n \r\n if (hasRelevantChanges) {\r\n // Reapply styles for all stories that have them\r\n storyStyles.forEach((styles, storyId) => {\r\n applyStylesToStory(storyId, styles);\r\n });\r\n }\r\n });\r\n\r\n observer.observe(root, {\r\n childList: true,\r\n subtree: true\r\n });\r\n}\r\n\r\nfunction cleanupObserver() {\r\n if (observer) {\r\n observer.disconnect();\r\n observer = null;\r\n }\r\n}\r\n\r\nfunction cleanupPreviousStoryStyles(previousStoryId: string, previousContext: any) {\r\n const previousStyles = storyStyles.get(previousStoryId);\r\n if (!previousStyles || !previousContext) {\r\n return;\r\n }\r\n\r\n const isDocsMode = previousContext?.viewMode === 'docs';\r\n const targetSelector = previousContext?.parameters?.cssVarsTarget;\r\n \r\n let elements: HTMLElement[] = [];\r\n \r\n if (isDocsMode) {\r\n // In docs mode, only clean up elements specific to the previous story\r\n if (targetSelector) {\r\n const storyContainers = document.querySelectorAll(`[data-docs-story=\"${previousStoryId}\"], [id=\"story--${previousStoryId}\"]`);\r\n storyContainers.forEach(container => {\r\n const targetElements = container.querySelectorAll<HTMLElement>(targetSelector);\r\n elements.push(...Array.from(targetElements));\r\n });\r\n } else {\r\n // Use smart detection for specific story containers\r\n const storyContainers = document.querySelectorAll(`[data-docs-story=\"${previousStoryId}\"], [id=\"story--${previousStoryId}\"]`);\r\n storyContainers.forEach(container => {\r\n const smartElement = findComponentInContainer(container as HTMLElement);\r\n if (smartElement) elements.push(smartElement);\r\n });\r\n }\r\n } else {\r\n // For regular story mode, clean up all elements since only one story is visible\r\n if (targetSelector) {\r\n const element = document.querySelector<HTMLElement>(`#storybook-root ${targetSelector}`);\r\n if (element) elements = [element];\r\n } else {\r\n // Use smart component detection\r\n const smartElement = findComponentInStoryRoot();\r\n if (smartElement) elements = [smartElement];\r\n }\r\n }\r\n \r\n // Remove all CSS variables that were set by the previous story\r\n elements.forEach(element => {\r\n Object.keys(previousStyles).forEach(property => {\r\n element.style.removeProperty(property);\r\n });\r\n });\r\n \r\n // Don't remove from storyStyles map in docs mode since multiple stories can be active\r\n if (!isDocsMode) {\r\n storyStyles.delete(previousStoryId);\r\n }\r\n}\r\n\r\n// Smart component detection for regular story mode\r\nfunction findComponentInStoryRoot(): HTMLElement | null {\r\n const root = document.getElementById('storybook-root');\r\n if (!root) return null;\r\n\r\n // Strategy 1: Look for elements that use CSS variables (most reliable)\r\n const elementWithCssVars = findElementUsingCssVariables(root);\r\n if (elementWithCssVars) return elementWithCssVars;\r\n\r\n // Strategy 2: Find the deepest meaningful element with classes\r\n const deepestElement = findDeepestMeaningfulElement(root);\r\n if (deepestElement) return deepestElement;\r\n\r\n // Strategy 3: Fallback to first child\r\n return root.firstElementChild as HTMLElement;\r\n}\r\n\r\n// Smart component detection within a specific container (for docs mode)\r\nfunction findComponentInContainer(container: HTMLElement): HTMLElement | null {\r\n // Strategy 1: Look for elements that use CSS variables\r\n const elementWithCssVars = findElementUsingCssVariables(container);\r\n if (elementWithCssVars) return elementWithCssVars;\r\n\r\n // Strategy 2: Find the deepest meaningful element with classes\r\n const deepestElement = findDeepestMeaningfulElement(container);\r\n if (deepestElement) return deepestElement;\r\n\r\n // Strategy 3: Fallback to first child\r\n return container.firstElementChild as HTMLElement;\r\n}\r\n\r\n// Find elements that are already using CSS variables (most likely the target)\r\nfunction findElementUsingCssVariables(container: HTMLElement): HTMLElement | null {\r\n const walker = document.createTreeWalker(\r\n container,\r\n NodeFilter.SHOW_ELEMENT,\r\n {\r\n acceptNode: (node: Element) => {\r\n if (node === container) return NodeFilter.FILTER_SKIP;\r\n \r\n const element = node as HTMLElement;\r\n const computedStyle = window.getComputedStyle(element);\r\n \r\n // Check if element uses any CSS custom properties\r\n for (let i = 0; i < computedStyle.length; i++) {\r\n const property = computedStyle[i];\r\n if (property && property.startsWith('--')) {\r\n return NodeFilter.FILTER_ACCEPT;\r\n }\r\n }\r\n \r\n // Also check if the element's CSS contains var() functions\r\n const styles = ['background', 'backgroundColor', 'color', 'border', 'borderColor'];\r\n for (const style of styles) {\r\n const value = computedStyle.getPropertyValue(style);\r\n if (value && value.includes('var(--')) {\r\n return NodeFilter.FILTER_ACCEPT;\r\n }\r\n }\r\n \r\n return NodeFilter.FILTER_SKIP;\r\n }\r\n }\r\n );\r\n\r\n return walker.nextNode() as HTMLElement | null;\r\n}\r\n\r\n// Find the deepest meaningful element (elements with classes or meaningful attributes)\r\nfunction findDeepestMeaningfulElement(container: HTMLElement): HTMLElement | null {\r\n let deepestElement: HTMLElement | null = null;\r\n let maxDepth = 0;\r\n\r\n const walker = document.createTreeWalker(\r\n container,\r\n NodeFilter.SHOW_ELEMENT,\r\n {\r\n acceptNode: (node: Element) => {\r\n if (node === container) return NodeFilter.FILTER_SKIP;\r\n \r\n const element = node as HTMLElement;\r\n \r\n // Consider any element with a class name as a potential component\r\n const hasClass = typeof element.className === 'string' && element.className.trim() !== '';\r\n const hasId = typeof element.id === 'string' && element.id !== '';\r\n \r\n if (hasClass || hasId) {\r\n // Calculate depth from container\r\n let depth = 0;\r\n let parent = element.parentElement;\r\n while (parent && parent !== container) {\r\n depth++;\r\n parent = parent.parentElement;\r\n }\r\n \r\n if (depth > maxDepth) {\r\n maxDepth = depth;\r\n deepestElement = element;\r\n }\r\n \r\n return NodeFilter.FILTER_ACCEPT;\r\n }\r\n \r\n return NodeFilter.FILTER_SKIP;\r\n }\r\n }\r\n );\r\n\r\n // Walk through all nodes to find the deepest\r\n while (walker.nextNode()) {\r\n // The logic is in the acceptNode function\r\n }\r\n\r\n return deepestElement;\r\n}\r\n","/**\r\n * A decorator is a way to wrap a story in extra “rendering” functionality. Many addons define decorators\r\n * in order to augment stories:\r\n * - with extra rendering\r\n * - gather details about how a story is rendered\r\n *\r\n * When writing stories, decorators are typically used to wrap stories with extra markup or context mocking.\r\n *\r\n * https://storybook.js.org/docs/react/writing-stories/decorators\r\n */\r\nimport type { ProjectAnnotations, Renderer } from \"storybook/internal/types\";\r\nimport { KEY } from \"./constants\";\r\nimport { withRoundTrip } from \"./withRoundTrip\";\r\n\r\n/**\r\n * Note: if you want to use JSX in this file, rename it to `preview.tsx`\r\n * and update the entry prop in tsup.config.ts to use \"src/preview.tsx\",\r\n */\r\n\r\nconst preview: ProjectAnnotations<Renderer> = {\r\n decorators: [withRoundTrip],\r\n initialGlobals: {\r\n [KEY]: {}\r\n },\r\n};\r\n\r\nexport default preview;\r\n"]}