Best JavaScript code snippet using storybook-root
stories.ts
Source:stories.ts
1import React from 'react';2import deprecate from 'util-deprecate';3import dedent from 'ts-dedent';4import { sanitize } from '@storybook/csf';5import mapValues from 'lodash/mapValues';6import { StoryId, StoryKind, Args, Parameters, combineParameters } from '../index';7import merge from './merge';8import { Provider } from '../modules/provider';9import { ViewMode } from '../modules/addons';10export type { StoryId };11export interface Root {12 id: StoryId;13 depth: 0;14 name: string;15 refId?: string;16 children: StoryId[];17 isComponent: false;18 isRoot: true;19 isLeaf: false;20 renderLabel?: (item: Root) => React.ReactNode;21 startCollapsed?: boolean;22}23export interface Group {24 id: StoryId;25 depth: number;26 name: string;27 children: StoryId[];28 refId?: string;29 parent?: StoryId;30 isComponent: boolean;31 isRoot: false;32 isLeaf: false;33 renderLabel?: (item: Group) => React.ReactNode;34 // MDX docs-only stories are "Group" type35 parameters?: {36 docsOnly?: boolean;37 viewMode?: ViewMode;38 };39}40export interface Story {41 id: StoryId;42 depth: number;43 parent: StoryId;44 name: string;45 kind: StoryKind;46 refId?: string;47 children?: StoryId[];48 isComponent: boolean;49 isRoot: false;50 isLeaf: true;51 renderLabel?: (item: Story) => React.ReactNode;52 parameters?: {53 fileName: string;54 options: {55 [optionName: string]: any;56 };57 docsOnly?: boolean;58 viewMode?: ViewMode;59 [parameterName: string]: any;60 };61 args: Args;62 initialArgs: Args;63}64export interface StoryInput {65 id: StoryId;66 name: string;67 refId?: string;68 kind: StoryKind;69 children: string[];70 parameters: {71 fileName: string;72 options: {73 [optionName: string]: any;74 };75 docsOnly?: boolean;76 viewMode?: ViewMode;77 [parameterName: string]: any;78 };79 isLeaf: boolean;80 args: Args;81 initialArgs: Args;82}83export interface StoriesHash {84 [id: string]: Root | Group | Story;85}86export type StoriesList = (Group | Story)[];87export type GroupsList = (Root | Group)[];88export interface StoriesRaw {89 [id: string]: StoryInput;90}91export type SetStoriesPayload =92 | {93 v: 2;94 error?: Error;95 globals: Args;96 globalParameters: Parameters;97 stories: StoriesRaw;98 kindParameters: {99 [kind: string]: Parameters;100 };101 }102 | ({103 v?: number;104 stories: StoriesRaw;105 } & Record<string, never>);106const warnLegacyShowRoots = deprecate(107 () => {},108 dedent`109 The 'showRoots' config option is deprecated and will be removed in Storybook 7.0. Use 'sidebar.showRoots' instead.110 Read more about it in the migration guide: https://github.com/storybookjs/storybook/blob/master/MIGRATION.md111 `112);113const warnChangedDefaultHierarchySeparators = deprecate(114 () => {},115 dedent`116 The default hierarchy separators changed in Storybook 6.0.117 '|' and '.' will no longer create a hierarchy, but codemods are available.118 Read more about it in the migration guide: https://github.com/storybookjs/storybook/blob/master/MIGRATION.md119 `120);121export const denormalizeStoryParameters = ({122 globalParameters,123 kindParameters,124 stories,125}: SetStoriesPayload): StoriesRaw => {126 return mapValues(stories, (storyData) => ({127 ...storyData,128 parameters: combineParameters(129 globalParameters,130 kindParameters[storyData.kind],131 (storyData.parameters as unknown) as Parameters132 ),133 }));134};135export const transformStoriesRawToStoriesHash = (136 input: StoriesRaw,137 { provider }: { provider: Provider }138): StoriesHash => {139 const values = Object.values(input).filter(Boolean);140 const usesOldHierarchySeparator = values.some(({ kind }) => kind.match(/\.|\|/)); // dot or pipe141 const storiesHashOutOfOrder = values.reduce((acc, item) => {142 const { kind, parameters } = item;143 const { sidebar = {}, showRoots: deprecatedShowRoots } = provider.getConfig();144 const { showRoots = deprecatedShowRoots, collapsedRoots = [], renderLabel } = sidebar;145 if (typeof deprecatedShowRoots !== 'undefined') {146 warnLegacyShowRoots();147 }148 const setShowRoots = typeof showRoots !== 'undefined';149 if (usesOldHierarchySeparator && !setShowRoots) {150 warnChangedDefaultHierarchySeparators();151 }152 const groups = kind.split('/').map((part) => part.trim());153 const root = (!setShowRoots || showRoots) && groups.length > 1 ? [groups.shift()] : [];154 const rootAndGroups = [...root, ...groups].reduce((list, name, index) => {155 const parent = index > 0 && list[index - 1].id;156 const id = sanitize(parent ? `${parent}-${name}` : name);157 if (parent === id) {158 throw new Error(159 dedent`160 Invalid part '${name}', leading to id === parentId ('${id}'), inside kind '${kind}'161 Did you create a path that uses the separator char accidentally, such as 'Vue <docs/>' where '/' is a separator char? See https://github.com/storybookjs/storybook/issues/6128162 `163 );164 }165 if (root.length && index === 0) {166 list.push({167 id,168 name,169 depth: index,170 children: [],171 isComponent: false,172 isLeaf: false,173 isRoot: true,174 renderLabel,175 startCollapsed: collapsedRoots.includes(id),176 });177 } else {178 list.push({179 id,180 name,181 parent,182 depth: index,183 children: [],184 isComponent: false,185 isLeaf: false,186 isRoot: false,187 renderLabel,188 parameters: {189 docsOnly: parameters?.docsOnly,190 viewMode: parameters?.viewMode,191 },192 });193 }194 return list;195 }, [] as GroupsList);196 const paths = [...rootAndGroups.map(({ id }) => id), item.id];197 // Ok, now let's add everything to the store198 rootAndGroups.forEach((group, index) => {199 const child = paths[index + 1];200 const { id } = group;201 acc[id] = merge(acc[id] || {}, {202 ...group,203 ...(child && { children: [child] }),204 });205 });206 acc[item.id] = {207 ...item,208 depth: rootAndGroups.length,209 parent: rootAndGroups[rootAndGroups.length - 1].id,210 isLeaf: true,211 isComponent: false,212 isRoot: false,213 renderLabel,214 };215 return acc;216 }, {} as StoriesHash);217 function addItem(acc: StoriesHash, item: Story | Group) {218 if (!acc[item.id]) {219 // If we were already inserted as part of a group, that's great.220 acc[item.id] = item;221 const { children } = item;222 if (children) {223 const childNodes = children.map((id) => storiesHashOutOfOrder[id]) as (Story | Group)[];224 acc[item.id].isComponent = childNodes.every((childNode) => childNode.isLeaf);225 childNodes.forEach((childNode) => addItem(acc, childNode));226 }227 }228 return acc;229 }230 return Object.values(storiesHashOutOfOrder).reduce(addItem, {});231};232export type Item = StoriesHash[keyof StoriesHash];233export function isRoot(item: Item): item is Root {234 if (item as Root) {235 return item.isRoot;236 }237 return false;238}239export function isGroup(item: Item): item is Group {240 if (item as Group) {241 return !item.isRoot && !item.isLeaf;242 }243 return false;244}245export function isStory(item: Item): item is Story {246 if (item as Story) {247 return item.isLeaf;248 }249 return false;...
stories.js
Source:stories.js
1import "core-js/modules/es.array.reduce.js";2import deprecate from 'util-deprecate';3import dedent from 'ts-dedent';4import { sanitize } from '@storybook/csf';5import mapValues from 'lodash/mapValues';6import { combineParameters } from '../index';7import merge from './merge';8const warnLegacyShowRoots = deprecate(() => {}, dedent`9 The 'showRoots' config option is deprecated and will be removed in Storybook 7.0. Use 'sidebar.showRoots' instead.10 Read more about it in the migration guide: https://github.com/storybookjs/storybook/blob/master/MIGRATION.md11 `);12const warnChangedDefaultHierarchySeparators = deprecate(() => {}, dedent`13 The default hierarchy separators changed in Storybook 6.0.14 '|' and '.' will no longer create a hierarchy, but codemods are available.15 Read more about it in the migration guide: https://github.com/storybookjs/storybook/blob/master/MIGRATION.md16 `);17export const denormalizeStoryParameters = ({18 globalParameters,19 kindParameters,20 stories21}) => {22 return mapValues(stories, storyData => Object.assign({}, storyData, {23 parameters: combineParameters(globalParameters, kindParameters[storyData.kind], storyData.parameters)24 }));25};26const STORY_KIND_PATH_SEPARATOR = /\s*\/\s*/;27export const transformStoriesRawToStoriesHash = (input, {28 provider29}) => {30 const values = Object.values(input).filter(Boolean);31 const usesOldHierarchySeparator = values.some(({32 kind33 }) => kind.match(/\.|\|/)); // dot or pipe34 const storiesHashOutOfOrder = values.reduce((acc, item) => {35 const {36 kind,37 parameters38 } = item;39 const {40 sidebar = {},41 showRoots: deprecatedShowRoots42 } = provider.getConfig();43 const {44 showRoots = deprecatedShowRoots,45 collapsedRoots = [],46 renderLabel47 } = sidebar;48 if (typeof deprecatedShowRoots !== 'undefined') {49 warnLegacyShowRoots();50 }51 const setShowRoots = typeof showRoots !== 'undefined';52 if (usesOldHierarchySeparator && !setShowRoots) {53 warnChangedDefaultHierarchySeparators();54 }55 const groups = kind.trim().split(STORY_KIND_PATH_SEPARATOR);56 const root = (!setShowRoots || showRoots) && groups.length > 1 ? [groups.shift()] : [];57 const rootAndGroups = [...root, ...groups].reduce((list, name, index) => {58 const parent = index > 0 && list[index - 1].id;59 const id = sanitize(parent ? `${parent}-${name}` : name);60 if (parent === id) {61 throw new Error(dedent`62 Invalid part '${name}', leading to id === parentId ('${id}'), inside kind '${kind}'63 Did you create a path that uses the separator char accidentally, such as 'Vue <docs/>' where '/' is a separator char? See https://github.com/storybookjs/storybook/issues/612864 `);65 }66 if (root.length && index === 0) {67 list.push({68 id,69 name,70 depth: index,71 children: [],72 isComponent: false,73 isLeaf: false,74 isRoot: true,75 renderLabel,76 startCollapsed: collapsedRoots.includes(id)77 });78 } else {79 list.push({80 id,81 name,82 parent,83 depth: index,84 children: [],85 isComponent: false,86 isLeaf: false,87 isRoot: false,88 renderLabel,89 parameters: {90 docsOnly: parameters === null || parameters === void 0 ? void 0 : parameters.docsOnly,91 viewMode: parameters === null || parameters === void 0 ? void 0 : parameters.viewMode92 }93 });94 }95 return list;96 }, []);97 const paths = [...rootAndGroups.map(({98 id99 }) => id), item.id]; // Ok, now let's add everything to the store100 rootAndGroups.forEach((group, index) => {101 const child = paths[index + 1];102 const {103 id104 } = group;105 acc[id] = merge(acc[id] || {}, Object.assign({}, group, child && {106 children: [child]107 }));108 });109 acc[item.id] = Object.assign({}, item, {110 depth: rootAndGroups.length,111 parent: rootAndGroups[rootAndGroups.length - 1].id,112 isLeaf: true,113 isComponent: false,114 isRoot: false,115 renderLabel116 });117 return acc;118 }, {});119 function addItem(acc, item) {120 if (!acc[item.id]) {121 // If we were already inserted as part of a group, that's great.122 acc[item.id] = item;123 const {124 children125 } = item;126 if (children) {127 const childNodes = children.map(id => storiesHashOutOfOrder[id]);128 acc[item.id].isComponent = childNodes.every(childNode => childNode.isLeaf);129 childNodes.forEach(childNode => addItem(acc, childNode));130 }131 }132 return acc;133 }134 return Object.values(storiesHashOutOfOrder).reduce(addItem, {});135};136export function isRoot(item) {137 if (item) {138 return item.isRoot;139 }140 return false;141}142export function isGroup(item) {143 if (item) {144 return !item.isRoot && !item.isLeaf;145 }146 return false;147}148export function isStory(item) {149 if (item) {150 return item.isLeaf;151 }152 return false;...
Using AI Code Generation
1const usesOldHierarchySeparator = require('storybook-root').usesOldHierarchySeparator;2const getStorybookRootPath = require('storybook-root').getStorybookRootPath;3const getStorybookRootPath = require('storybook-root').getStorybookRootPath;4const getStorybookRootPath = require('storybook-root').getStorybookRootPath;5const getStorybookRootPath = require('storybook-root').getStorybookRootPath;6const getStorybookRootPath = require('storybook-root').getStorybookRootPath;7const getStorybookRootPath = require('storybook-root').getStorybookRootPath;8const getStorybookRootPath = require('storybook-root').getStorybookRootPath;9const getStorybookRootPath = require('storybook-root').getStorybookRootPath;10const getStorybookRootPath = require('storybook-root').getStorybookRootPath;11const getStorybookRootPath = require('storybook-root').getStorybookRootPath;12const getStorybookRootPath = require('storybook-root').getStorybookRootPath;13const getStorybookRootPath = require('storybook-root').getStorybookRootPath;14const getStorybookRootPath = require('storybook-root').getStorybookRootPath;15const getStorybookRootPath = require('storybook-root').getStorybookRootPath;
Using AI Code Generation
1import { usesOldHierarchySeparator } from 'storybook-root';2console.log(usesOldHierarchySeparator());3import { isStorybook } from 'storybook-root';4console.log(isStorybook());5import { getStorybook } from 'storybook-root';6console.log(getStorybook());7import { getStorybookUI } from 'storybook-root';8console.log(getStorybookUI());9import { getStorybookUIParams } from 'storybook-root';10console.log(getStorybookUIParams());11import { getStorybookUIExtra } from 'storybook-root';12console.log(getStorybookUIExtra());13import { getStorybookUIRoot } from 'storybook-root';14console.log(getStorybookUIRoot());15import { getStorybookUIRN } from 'storybook-root';16console.log(getStorybookUIRN());17import { getStorybookUIRNParams } from 'storybook-root';18console.log(getStorybookUIRNParams());19import { getStorybookUIRNExtra } from 'storybook-root';20console.log(getStorybookUIRNExtra());21import { getStorybookUIRNRoot } from 'storybook-root';22console.log(getStorybookUIRNRoot());23import { getStorybookUIWeb } from 'storybook-root';24console.log(getStorybookUIWeb());25import { getStorybookUIWebParams } from 'storybook-root';26console.log(getStorybookUIWebParams());27import { getStorybookUIWebExtra } from 'storybook-root';28console.log(getStorybookUI
Using AI Code Generation
1import {usesOldHierarchySeparator} from 'storybook-root';2console.log(usesOldHierarchySeparator());3import {configure} from '@storybook/react';4import {setOptions} from 'storybook-root';5setOptions({6});7configure(loadStories, module);8const path = require('path');9module.exports = (baseConfig, env, config) => {10 config.resolve.alias['storybook-root'] = path.resolve(__dirname, '../');11 return config;12};13import '@storybook/addon-actions/register';14import '@storybook/addon-links/register';15import {configure} from '@storybook/react';16const req = require.context('../src', true, /.stories.js$/);17function loadStories() {18 req.keys().forEach(filename => req(filename));19}20configure(loadStories, module);21import {configure} from '@storybook/react';22const req = require.context('../src', true, /.stories.js$/);23function loadStories() {24 req.keys().forEach(filename => req(filename));25}26configure(loadStories, module);27import {configure} from '@storybook/react';28import {setOptions} from '@storybook/addon-options';29setOptions({30});31configure(loadStories, module);32import {configure} from '@storybook/react';33import {setOptions} from '@storybook/addon-options';34setOptions({35});36configure(loadStories, module);37import {configure} from '@storybook/react';38import {setOptions} from '@storybook/addon-options';39setOptions({
Using AI Code Generation
1import { usesOldHierarchySeparator } from '@storybook/react/dist/client/preview/start';2console.log(usesOldHierarchySeparator());3export var usesOldHierarchySeparator = function usesOldHierarchySeparator() {4 var root = getStorybookRootData();5 return root.usesOldHierarchySeparator;6};7function getStorybookRootData() {8 var root = window.__STORYBOOK_CLIENT_API__.storyStore.getRoot();9 return root;10}11 configApi = _window$__STORYBOOK_C.configApi;12 configApi = _window$__STORYBOOK_C.configApi;13 configApi = _window$__STORYBOOK_C.configApi;14 configApi = _window$__STORYBOOK_C.configApi;
Using AI Code Generation
1addDecorator(withKnobs);2import { withKnobs } from '@storybook/addon-knobs';3import { addDecorator } from '@storybook/react';4addDecorator(withKnobs);5import { withKnobs } from '@storybook/addon-knobs';6import { addDecorator } from '@storybook/react';7addDecorator(withKnobs);8import { withKnobs } from '@storybook/addon-knobs';9import { addDecorator } from '@storybook/react';10addDecorator(withKnobs);11import { withKnobs } from '@storybook/addon-knobs';12import { addDecorator } from '@storybook/react';13addDecorator(withKnobs);14import { withKnobs } from '@storybook/addon-knobs';15import { addDecorator } from '@storybook/react';16addDecorator(withKnobs);17import { withKnobs } from '@storybook/addon-knobs';18import { addDecorator } from '@storybook/react';19addDecorator(withKnobs);20import { withKnobs } from '@storybook/addon-knobs';21import { addDecorator } from '@storybook/react';22addDecorator(withKnobs);23import { withKnobs } from '@storybook/addon-knobs';24import { addDecorator } from '@storybook/react';25addDecorator(withKnobs);26import { withKnobs } from '@storybook/addon-knobs';27import { addDecorator
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!