Storyteller
Storyteller is a library for the discovery and rendering of UI stories and powers our storybook plugin Flipbook.
The API for this package focuses around the following areas:
- Validation for the Story format and Storybook format
- Discovery of valid ModuleScripts with
.storyand.storybookextensions - Loading of Stories and Storybooks into a sandbox with cacheless module requiring
- Rendering stories into a container with lifecycle callbacks for updating and unmounting
There also exist React hooks for ease of integration into storybook apps.
Functions
findStoryModules
StoryDiscoveryDiscovers all Story modules that are descendants of parent.
findStoryModulesForStorybook
StorybookStoryDiscoveryDiscovers all Story modules that are managed by the given Storybook.
getLoadedStorybooks
StorybookStoryteller.getLoadedStorybooks() → {LoadedStorybook}Returns the Storybooks loaded by Storyteller's shared runtime.
This is a Charm getter for the shared runtime only; it does not read React context or start discovery. Mount a StorytellerProvider without a custom store, or use a Storyteller hook outside a provider, to start shared discovery.
A provider with a custom store and its descendant hooks use that store instead. They do not start the shared runtime or supply results to this getter.
render
RenderingStoryteller.render() → RenderLifecycleRender a Story to an Instance in the DataModel.
After discovering, validating, and loading Story modules, rendering them is the final step to getting stories visually presented to the user.
This function handles the lifecycle of mounting, updating, and unmounting the Story. On each update, controls can be passed down to the story for live-reloading from user interaction.
Usage:
local ModuleLoader = require("@pkg/ModuleLoader")
local Storyteller = require("@pkg/Storyteller")
-- At least one `.storybook` module must be present
local storybookModules = Storyteller.findStorybookModules(game)
assert(#storybookModules > 0, "no Storybook modules found")
local storybook
pcall(function()
storybook = Storyteller.loadStorybookModule(storybookModules[1])
end)
if storybook then
-- At least one `.story` module must be a descendant of the Instances in
-- a Storybook's `storyRoots` array
local storyModules = Storyteller.findStoryModulesForStorybook(storybook)
assert(#storyModules > 0, "no Story modules found")
local story
pcall(function()
story = Storyteller.loadStoryModule(storyModules[1], storybook)
end)
if story then
-- Finally, render the story to a container of your choosing
local lifecycle = Storyteller.render(container, story)
print(container:GetChildren())
lifecycle.unmount()
print(container:GetChildren())
end
end
getOrphanedStoryModules
StoryReturns Story modules in the shared runtime outside the roots of every loaded Storybook.
This is a Charm getter for the shared runtime only; it does not read React context or start discovery. Mount a StorytellerProvider without a custom store, or use a Storyteller hook outside a provider, to start shared discovery.
A provider with a custom store and its descendant hooks use that store instead. They do not start the shared runtime or supply results to this getter.
requestStoryModule
StoryModule LoadingStoryteller.requestStoryModule(request: StoryRequest) → () → ()Asks the shared runtime to load a Story module, and returns a function that releases the request.
The module loads after this function returns. Call getStoryModuleRecord with the same request to read the result,
or pass that call to Charm.listen to be told when it changes. While the request is held, the module reloads when
it or its dependencies are edited.
Requests for the same module and Storybook share one record, including requests from hooks that use the shared
runtime. Passing a different storybook table for the same source counts as a new version of that Storybook, so the
module reloads.
The record is dropped when its last request is released. So to change a request's priority, make the new request before releasing the old one. If the old one was the last, releasing it first would leave no record until the module loads again.
local request = {
storyModule = storyModule,
storybook = storybook,
priority = "Interactive",
}
local release = Storyteller.requestStoryModule(request)
local stopListening = Charm.listen(function()
return Storyteller.getStoryModuleRecord(request)
end, function(record)
print(if record then record.status else "Loading")
end)
-- When the record is no longer needed
stopListening()
release()
This starts the shared runtime and keeps it running until the request is released. It does not read React context,
so a provider with a custom store never sees the request.
isStoryModule
ValidationStoryValidates a given Instance is a Story.
No validation is performed on the internals of the module. Only name and class are checked.
See Storyteller.loadStoryModule for validation of the module source.
loadStoriesFromModule
StoryModule LoadingStoryteller.loadStoriesFromModule() → {LoadedStory<T>}Loads every Story defined by a Story module.
A Story module can define a single Story, or a group of Stories under
stories as an array or a dictionary. Group-level summary, controls,
packages, and props become defaults for each Story. Stories are returned
in array order, or sorted by key for dictionaries. See the
Story format for details.
A ModuleLoader instance is used under the hood for handling the requiring of the Story module.
This function will throw if the module does not conform to the Story
format, if two Stories in the module share an id, or if the source has a
syntax error that require would normally fail for.
loadStorybookModule
StorybookModule LoadingStoryteller.loadStorybookModule(providedLoader: ModuleLoader?) → LoadedStorybookLoads the source of a Storybook module.
A ModuleLoader instance is required for handling the requiring of the module.
This function will throw if the return value of storybookModule does not
conform to Storybook format, or if the
source has a syntax error that require would normally fail for.
loadStoryModule
StoryModule LoadingStoryteller.loadStoryModule() → LoadedStory<T>Loads the source of a Story module.
A ModuleLoader instance is used under the hood for handling the requiring of the Story module.
This function will throw if the return value of storyModule does not
conform to the Story format, or if the source
has a syntax error that require would normally fail for.
For legacy compatibility this function also loads Hoarcekat
stories. Instead of a usual table-based Story definition, it takes the
returned function and wraps it in a Story, making the story field the
function body.
useOrphanedStoryModules
ReactStoryThis hook is used for the discovery of Story modules that are not associated with any Storybooks. These are known as "orphaned" or "unknown" stories
This hook should be used in conjunction with useStorybooks so that a list
of the available storybooks is determined first, otherwise all stories would
come back as orphans.
INFO
In the future version hooks may be migrated to a new package to remove the React dependency from Storyteller.
Usage:
local React = require("@pkg/React")
local Storyteller = require("@pkg/Storyteller")
local e = React.createElement
local function OrphanList(props: {
parent: Instance,
})
local storybooks = Storyteller.useStorybooks(props.parent)
local orphanedStoryModules = Storyteller.useOrphanedStoryModules(props.parent, storybooks.available)
local children = {}
for index, orphanedStoryModule in orphanedStoryModules do
children[orphanedStoryModule.Name] = e("TextLabel", {
Text = orphanedStoryModule.Name,
LayoutOrder = index,
}),
end
return e("Frame", {
Size = UDim2.fromScale(1, 1),
BackgroundTransparency = 1,
}, {
Layout = e("UIListLayout", {
SortOrder = Enum.SortOrder.LayoutOrder
}),
}, children)
end
return OrphanList
useStory
ReactStoryStoryteller.useStory() → (types.LoadedStory<unknown>?,string?)
This hook triggers a rerender when the Story module or any of its required
modules change. For example, updating the story property or updating a
React component’s source will trigger useStory to rerender with the new
content.
INFO
In the future version hooks may be migrated to a new package to remove the React dependency from Storyteller.
Usage:
local React = require("@pkg/React")
local Storyteller = require("@pkg/Storyteller")
local useEffect = React.useEffect
local useRef = React.useRef
local e = React.createElement
local function StoryView(props: {
parent: Instance,
storyModule: ModuleScript,
storybook: Storybook,
storyId: string?,
})
local ref = useRef<<Frame?>>(nil)
local story = Storyteller.useStory(props.storyModule, props.storybook, props.storyId)
useEffect(function()
if ref.current then
local renderer = Storyteller.createRendererForStory(story)
Storyteller.render(renderer, ref.current, story)
end
end, { story })
return e("Frame", {
Size = UDim2.fromScale(1, 1),
BackgroundTransparency = 1,
ref = ref,
})
end
return StoryView
useStorybooks
ReactStorybookStoryteller.useStorybooks(parent: Instance?) → {available: {LoadedStorybook},unavailable: {UnavailableStorybook},}Performs all the discovery and loading of Storybook modules that would normally be done via individual API members.
This hook makes it possible to conveniently load (and reload) Storybooks for use in React UI.
INFO
In the future version hooks may be migrated to a new package to remove the React dependency from Storyteller.
Usage:
local React = require("@pkg/React")
local Storyteller = require("@pkg/Storyteller")
local e = React.createElement
local function StorybookList(props: {
parent: Instance,
})
local storybooks = Storyteller.useStorybooks(props.parent)
local children = {}
for index, storybook in storybooks do
children[storybook.name] = e("TextLabel", {
Text = storybook.name,
LayoutOrder = index,
}),
end
return e("Frame", {
Size = UDim2.fromScale(1, 1),
BackgroundTransparency = 1,
}, {
Layout = e("UIListLayout", {
SortOrder = Enum.SortOrder.LayoutOrder
}),
}, children)
end
return StorybookList
This hook triggers a rerender when a Storybook module changes. For example,
updating the storyRoots of a Storybook will trigger a rerender, and when
paired with useStory you can get live updates to which Stories a Storybook
manages.
findOrphanedStoryModules
StorybookStoryDiscoveryDiscovers all Story modules that do not have a Storybook.
These are all of the Story modules that are not descendants of an instance
in the storyRoots array of the given storybooks.
findStorybookModules
StorybookDiscoveryDiscovers all Storybook modules that are descendants of parent.
This is the first step in the discovery of Stories. Once you load a
Storybook, you can then use its storyRoots array to discover all the
Stories it manages.
getUnavailableStorybooks
StorybookStoryteller.getUnavailableStorybooks() → {UnavailableStorybook}Returns the Storybooks Storyteller's shared runtime could not load.
This is a Charm getter for the shared runtime only; it does not read React context or start discovery. Mount a StorytellerProvider without a custom store, or use a Storyteller hook outside a provider, to start shared discovery.
A provider with a custom store and its descendant hooks use that store instead. They do not start the shared runtime or supply results to this getter.
getStoryModuleRecord
StoryModule LoadingStoryteller.getStoryModuleRecord(request: StoryRequest) → StoryModuleRecord?Returns the record of a Story module in the shared runtime, or nil when there isn't one yet.
The runtime makes a record after it loads a module that something requested, and drops it when the last request is
released. Call requestStoryModule first, or use a hook that requests the module, such as useStory. While a
module reloads after an edit, this returns the previous record.
The record's status says whether the module loaded. A Loaded record has name and stories, and an Unavailable
record has problem. When an edit to a module that already loaded makes it fail to load, the record stays Loaded with
the last working stories and also sets problem to the error.
local record = Storyteller.getStoryModuleRecord(request)
if record and record.status == "Loaded" then
print(record.name, #record.stories)
if record.problem then
warn("showing the last working version:", record.problem)
end
elseif record and record.status == "Unavailable" then
warn(record.problem)
end
This is a Charm getter for the shared runtime only; it does not read React context. A provider with a custom store
and its descendant hooks use that store instead, and do not supply records to this getter.
isStorybookModule
ValidationStorybookValidates a given Instance is a Storybook module.
No validation is performed on the internals of the module. Only name and class are checked.
See Storyteller.loadStorybookModule for validation of the module source.