Store
The Store module @component-controls/store provides access to the in-memory store of documents, stories, components, and react hooks to get access to this data.
The root
Store
object provides access to the global store of documents and data. interface Store {/*** global configuration for config file*/config?: RunConfiguration;/*** list of documents (pages)*/docs: StoreDocuments;/*** list of stories*/stories: Stories;/*** list of components used in stories and documents*/components: Components;/*** list of package.json files and their data* used by the components and the stories of the project*/packages: Packages;}
Returns the global store object
useStore = (): Store
import { useStore } from '@component-controls/store';{const store = useStore();console.log(store.config.theme);}
Returns the configuration object part of the store
useConfig = (): Configuration
import { useConfig } from '@component-controls/store';{const store = useConfig();console.log(config.theme);}
Returns the currently active tab for documents that have multiple tabs/pages
useActiveTab = (): string;
active tab
Returns a package object from a package id. The package id can come from a Document or a Component object.
usePackage = (packageId?: string): PackageInfo
import { usePackage } from '@component-controls/store';{const store = usePackage('some-package-id');console.log(config.theme);}
Retrieves a Document object from a document id
useDocument = (docId: string) => Document
import { useDocument } from '@component-controls/store';{const doc = useDocument('Introduction/Getting Started');console.log(doc.title);}
Returns the currently selected document
useCurrentDocument = (): Document
import { useCurrentDocument } from '@component-controls/store';{const doc = useCurrentDocument();console.log(doc.title);}
Returns the package information for the currently selected document
useDocPackage = (): PackageInfo
import { useDocPackage } from '@component-controls/store';{const pckg = useDocPackage();console.log(pckg.dependencies);}
Returns a key-value object of all documents in the store
useDocs = () => Record<string, Document>
import { useDocs } from '@component-controls/store';{const docs = useDocs();console.log(Object.keys(docs));}
Returns an array of all documents in the store
usePages = () => Document[]
import { usePages } from '@component-controls/store';{const pages = usePages();console.log(pages.map(page => page.title));}
Returns the doc sort order and a setter to update the sort order for a specific DocType
useDocSort = (type: DocType): [DocSortOrder, (newOrder: DocSortOrder) => void]
import { useDocSort } from '@component-controls/store';{const [sortOrder, setSortOrder] = useDocSort('blogs');console.log(sortOrder);<button onClick={() => setSortOrder('title')}>by title</button>}
Returns an array of all documents of a specific DocType
useDocByType = (type: DocType): Document[]
import { useDocByType } from '@component-controls/store';{const blogs = useDocByType('blogs');console.log(blogs.map(blog => blog.title));}
Returns a sorted list of documents of a specific DocType. Uses the sort order state.
useSortedDocByType = (type: DocType): Document[]
import { useSortedDocByType } from '@component-controls/store';{const blogs = useSortedDocByType('blogs');console.log(blogs.map(blog => blog.title));}
Returns a global object of key/value pairs with counts of documents per DocType
useDocTypeCount = (): Record<DocType, { count: number; home?: Document }>
import { useDocTypeCount } from '@component-controls/store';{const counts = useDocTypeCount('blogs');console.log(counts['blogs'].count);}
Returns the next/previous page objects for the current document
useNavigationInfo = (): NavigationResult
import { useNavigationInfo } from '@component-controls/store';{const nav = useNavigationInfo('blogs');console.log(nav.nextPage.link, nav.nextPage.title);}
Returns a link to a document from a DocType, document id, and active tab
useDocumentPath: = (type?: DocType, docId: string, activeTab?: string ) => string
import { useDocumentPath } from '@component-controls/store';{const href = useDocumentPath('blogs', 'Introduction/Getting Started');return <a href={href}>Visit tutorial</a>}
Returns the descript for a document page. It uses the doc.description property if available, or if there is a component assigned to the document will return the component's name.
useDocDescription = (doc?: Document): string
import { useDocument, useDocDescription } from '@component-controls/store';{const doc = useDocument('Introduction/Getting Started');const description = useDocDescription(doc);console.log(description);}
Retrieves a Story object from a story id
useStoryById = (storyId: string) => Story
import { useStoryById } from '@component-controls/store';{const story = useStoryById('esm-starter--overview');console.log(story.description);}
Returns the currently selected story
useCurrentStory = (): Story
import { useCurrentStory } from '@component-controls/store';{const story = useCurrentStory();console.log(story.description);}
Returns a story from an input id or story name. The id can be '.', which means the current story.
useStory = ({ id: string, name: string}): Story
import { useStory } from '@component-controls/store';{const story = useStory({ id: '.' });console.log(story.description);}
Returns a link to a story from a story id
useStoryPath = (storyId: string): string
import { useStoryPath } from '@component-controls/store';{const href = useStoryPath('esm-starter--overview');return <a href={href}>Visit tutorial</a>}
If the
of
property is specified, will return the listed components, if of
is not specified, will return the components specified in the component and subcomponent properties of the document or the story.useComponents = ({ of: string, name: string}): Componentsimport { useComponents } from '@component-controls/store';{const components = useComponents({ of: '.' });console.log(Object.keys(components));}
If the
of
property is specified, will return the listed component, if of
is not specified, will return the component specified in the component property of the document or the story.useComponent = ({ of: string, name: string}): Componentimport { useComponent } from '@component-controls/store';import { Button } from '../Button';{const component = useComponent({ of: Button });console.log(component.source);}
Returns a story's component from an input id or story name. The id can be '.', which means the current story.
useStoryComponent = ({ id: string, name: string}): Componentimport { useStoryComponent } from '@component-controls/store';{const component = useStoryComponent({ id: '.' });console.log(component.source);}
Returns the number of prop-info properties count for the current document. If the current document has more than one component assigned - will return the sum.
useCurrentPropsCount = (): numberimport { useCurrentPropsCount } from '@component-controls/store';{const count = useCurrentPropsCount();console.log(count);}
Returns the controls associated with a story
useStoryControls = (storyId: string): ComponentControls
import { useStoryControls } from '@component-controls/store';{const controls = useStoryControls('esm-starter--overview');console.log(controls);}
Returns a controls object and a setter function from the current control's context, given a control name. This hook is to be used when creating custom control editors.
useControl = (name: string): [T, (value: any) => void]
import { ControlTypes, ComponentControlBoolean } from '@component-controls/core';import { PropertyEditor } from '@component-controls/editors';import { useControl } from '@component-controls/store';const CheckboxEditor: PropertyEditor = ({ name }) => {const [control, onChange] = useControl<ComponentControlBoolean>(name);return (<inputtype="checkbox"id={name}onChange={(e: ChangeEvent<HTMLInputElement>) => {onChange(e.target.checked);}}checked={control.value ?? false}/>);};addPropertyEditor(ControlTypes.BOOLEAN, CheckboxEditor);
Returns the number of documents by unique values in their
category
fielduseDocPropCount = (category: string): Record<string, number>
import { useDocPropCount } from '@component-controls/store';{const authors = useDocPropCount('author');console.log(authors);}
Returns an array of documents that have a specific value in their
category
fielduseDocsByCategory = (category: string, value?: any): Document[]
import { useDocsByCategory } from '@component-controls/store';{const pages = useDocsByCategory('author', 'atanasster');console.log(pages);}