Stories Core

The engine behind @reelkit/react-stories-player. Pure TypeScript, no framework deps. Use it to build stories players for Angular, Vue, or vanilla JS.

Framework-Agnostic
Pure TypeScript, zero DOM framework deps
Two-Level Navigation
Groups + stories within each group
RAF Timer
requestAnimationFrame auto-advance with pause/resume
Canvas Progress
Retina-ready segmented progress bar with sliding window
Tap Zones
Configurable left/right tap detection
Reactive Signals
Built on @reelkit/core signal primitives

Installation

bash

Stories Controller

createStoriesController(config, events?) manages navigation across groups and stories. Tracks pause/resume state, remembers the last viewed story per group, and fires callbacks on each transition.

Config (StoriesControllerConfig)

PropertyTypeDefaultDescription
groupCountnumberrequiredTotal number of story groups
storyCountsnumber[]requiredNumber of stories in each group
initialGroupIndexnumber0Initial group index
initialStoryIndexnumber0Initial story index within the group
defaultImageDurationnumber5000Default auto-advance duration for image stories in ms

Events (StoriesControllerEvents)

EventTypeDescription
onStoryChange(groupIndex, storyIndex) => voidFired when the active story changes
onGroupChange(groupIndex) => voidFired when the active group changes
onStoryViewed(groupIndex, storyIndex) => voidFired when a story becomes visible
onStoryComplete(groupIndex, storyIndex) => voidFired when a story's timer completes (before advancing)
onComplete() => voidFired when the last story of the last group finishes
onClose() => voidFired when the overlay should close

State (reactive signals)

SignalTypeDescription
state.activeGroupIndexSignal<number>Currently active group index
state.activeStoryIndexSignal<number>Currently active story index within the group
state.isPausedSignal<boolean>Whether auto-advance is paused

Methods

MethodTypeDescription
nextStory()() => voidAdvance within group; crosses boundary to next group
prevStory()() => voidGo back within group; crosses boundary to prev group
nextGroup()() => voidSwitch to next group, resuming at last viewed story
prevGroup()() => voidSwitch to previous group, resuming at last viewed story
goToGroup(index)(number) => voidJump to a specific group by index
pause()() => voidPause auto-advance
resume()() => voidResume auto-advance
onStoryTimerComplete()() => voidCalled when the timer finishes; fires onStoryComplete then advances
getLastStoryIndex(groupIndex)(number) => numberLast viewed story index for a group (0 if never visited)
dispose()() => voidClean up resources

Example

typescript

Timer Controller

createTimerController(config) drives auto-advance with a requestAnimationFrame loop. The progress signal (0 to 1) feeds the progress bar. Pause and resume preserve the exact position.

Config (TimerControllerConfig)

PropertyTypeDefaultDescription
durationnumberrequiredDefault duration in milliseconds
onComplete() => voidundefinedCalled when the timer reaches 100%

State

SignalTypeDescription
progressSignal<number>Progress signal (0 to 1)
isRunningSignal<boolean>Whether the timer is currently running

Methods

MethodTypeDescription
start(duration?)(number?) => voidStart (or restart) the timer with an optional duration override
pause()() => voidFreeze progress at the current position
resume()() => voidContinue from the frozen position
reset()() => voidReset progress to 0 and stop
dispose()() => voidClean up resources

Example

typescript

Canvas Progress Renderer

createCanvasProgressRenderer(config?) draws segmented progress bars on a canvas. Scales for Retina displays, measures its container via ResizeObserver, and uses a sliding window when segments don't fit.

Config (CanvasProgressRendererConfig)

PropertyTypeDefaultDescription
gapnumber2Gap in pixels between segments
barHeightnumber2Bar height in pixels
minSegmentWidthnumber8Minimum segment width before the sliding window kicks in
bgColorstring'rgba(255,255,255,0.3)'Background color of unfilled segments
fillColorstring'#ffffff'Fill color of completed/active segments

Methods

MemberTypeDescription
attach(canvas)(HTMLCanvasElement) => voidAttach to a canvas element; starts ResizeObserver on parent
draw(totalStories, activeIndex, progress)(number, number, number) => voidDraw the progress bar for the given state
widthnumber (readonly)Current measured width in CSS pixels
dispose()() => voidClean up ResizeObserver and internal state

Example

typescript

Utility Functions

Pure functions for tap zone detection and progress bar math.

FunctionTypeDescription
getTapAction(tapX, containerWidth, splitRatio?)(number, number, number?) => 'prev' | 'next'Determines whether a tap triggers 'prev' or 'next' based on position. Default splitRatio is 0.3.
getSegments(totalStories, activeIndex, progress)(number, number, number) => SegmentState[]Computes status and fill percentage of each segment in a progress bar
getVisibleWindow(totalStories, activeIndex, progress, containerWidth, minSegmentWidth?, gap?)(number, number, number, number, number?, number?) => VisibleWindowComputes the visible sliding window of segments when total count exceeds container capacity

Types

All type definitions exported from @reelkit/stories-core.

typescript