The React UI
The optional React UI, @ifloom/react, with mountGame and its options, its two entries, its pages and its strings.
@ifloom/react is the optional React UI, so everything here is React only. To render a game in plain DOM, Vue or React by hand, see Start your own UI. Choosing a UI says when to use which.
@ifloom/react renders a game in the browser with React components built on shadcn/ui. It reads the view and sends commands through a runner, as a UI of your own would, and nothing in the engine depends on it. Set up the React UI installs it and writes the files a game needs. In the showcase: examples/showcase/src/main.tsx.
The package has two entries:
@ifloom/reactholdsmountGame,GameShell, the layout's regions, every game component and hook, the options that restyle them, andcheckUiHints. Its exports are in@ifloom/react.@ifloom/react/kitholds what a component of your own is built from: a plugin's UI, the kit's parts, dialogs and pages like the UI's own, and the shadcn/ui primitives. Its exports are in@ifloom/react/kit.
The React UI's reference has five pages:
- This page documents
mountGame,MountOptions,GameShelland the UI's own strings. - Components documents every component and hook of
@ifloom/react, from the layout's regions to the pages around the game. - Plugin UIs and the kit documents
PluginUi,pluginKit,usePlugin, the kit's parts and the shadcn/ui primitives, from@ifloom/react/kit. - Presentation hints documents the
uihints and inline pieces the React UI reads from content, andcheckUiHints. - React UI parts lists every
data-partthe UI renders, with the attributes each carries.
mountGame
mountGame starts a game in the page and resolves to its runner once the game renders. Its signature is in mountGame.
It takes a Game, or the { game, problems } a game module's build() returns, and logs any problems as warnings. It makes the runner with browserRunner, so the game has undo, saves, an autosave and the player's stored settings. It then renders a GameShell inside React's StrictMode into #root, unless root names another element. It rejects when nothing in the page matches root.
mountGame is async because browserRunner reads the player's profile first, so the game starts with their settings. A game that never sends a command from outside the UI calls it without await.
import { mountGame } from '@ifloom/react'
import module from './game.ts'
import { Layout } from './layout.tsx'
const runner = await mountGame(module.build(), {
runner: { backlog: { limit: 200 } },
layout: Layout,
})Set up the React UI mounts a new game with it.
MountOptions
MountOptions is the settings for mountGame: every prop of GameShell but runner, with where to render and how to start. Every field is in MountOptions.
| Field | Meaning |
|---|---|
root | The element to render into, or a selector for it. '#root' if left out. |
start | The state the game starts from. A new game with a random seed and the player's stored play settings if left out, so each playthrough draws its own variants. |
runner | The runner options, merged over browserRunner's defaults: undo 50 deep, saves and the profile in localStorage, and an autosave each turn. |
Every other field goes to GameShell as a prop, such as layout, pages, plugins or keys.
GameShell
GameShell renders the whole React UI for a runner, and mountGame renders one too. Its props are in GameShellProps.
It renders a GameProvider with the game's sound, its text size and, with pages on, the pages, around the layout. Its props are the provider's, which GameProvider and UiOptions describe, and these three:
| Field | Meaning |
|---|---|
layout | The page as a component, such as your src/layout.tsx. DefaultLayout if left out. Any component works, since every hook and built-in component works inside it. |
pages | The title screen and the pages around the game, true or a PagesOptions. Off if left out, and the game starts at once. With pages on, the layout renders on the game page. |
audio | The game's sound through the audio player. It starts when game.audio lists any tracks, and false turns it off. An object goes to startAudio as its options. |
The shell renders again whenever the runner's snapshot changes, so an undo, a loaded save and a command sent from outside the UI all show at once. Render it yourself when you make the runner yourself, as the showcase does to load the player's mods before it builds the game:
import { createRoot } from 'react-dom/client'
import { localStorageDriver } from '@ifloom/browser'
import { createRunner, profile, saves, startingState } from '@ifloom/engine'
import { GameShell } from '@ifloom/react'
import module from './game.ts'
import './styles.css'
const { game } = module.build()
const runner = createRunner(game, startingState(game, { seed: Math.floor(Math.random() * 2 ** 31) }), {
saves: saves(game, localStorageDriver()),
profile: profile(game, localStorageDriver()),
history: { depth: 50 },
autosave: { every: 'turn' },
})
createRoot(document.getElementById('root')!).render(<GameShell runner={runner} pages />)Rearrange or replace the React UI's parts writes a layout of your own.
Strings
The React UI's own words come from the game's strings table, under keys that start with ui.. A game rewords any of them in its content's strings, and useWords reads one inside a component. Each plugin UI's words are reworded the same way and listed under ## React UI on its plugin's page, such as ui.quests for the quests plugin's "Quests".
import type { Content } from '@ifloom/engine'
export const words: Content = {
strings: {
'ui.stats': 'How you are',
'ui.quests': 'To do',
},
}| Key | Meaning |
|---|---|
ui.stats | Stats, the stats section's title and the stats panel's. |
ui.statLevel | Level {level}, after a levelled stat's label in the StatList. |
ui.statProgress | {label}: {percent}% of the way to level {next}, the label on a levelled stat's bar. |
ui.allSeen | Mark all seen, the button in the NoticeStrip's dialog. |
ui.back | Back, the button on the settings, load and credits pages. |
ui.dismiss | Dismiss, the label on each notice's dismiss button. |
ui.undo, ui.redo | Undo and Redo, the GameMenu's buttons. |
ui.saves, ui.save, ui.saveNew, ui.load | Saves, Save, Save in a new slot and Load, in the SavesMenu. |
ui.remove | Delete, the button that deletes a save or removes a mod. |
ui.exportSave, ui.importSave | Export and Import a save file, in the saves menu and on the load page. |
ui.noSaves, ui.brokenSave | No saves yet. and A save that can’t be read, in the saves menu and on the load page. |
ui.missingPacks | This save was made with mods this game doesn’t have: {packs}. |
ui.missingPlugins | This save was made with plugins this game doesn’t have: {plugins}. |
ui.backlog, ui.backlogEmpty, ui.chose | Backlog, Nothing yet. and You chose: {label}, in the Backlog. |
ui.enter | Enter, the button on an input. |
ui.mods, ui.addMod, ui.noMods | Mods, Add a mod file and No mods added., in the ModManager. |
ui.modOn, ui.modOff | On and Off, a mod's state button. |
ui.applyMods, ui.modsChanged | Restart with these mods and Changes to mods take effect when the game restarts. |
ui.modsHint | Mods add to the game or change it. Add one from a file, and turn it on or off here., under the settings page's Mods heading. |
ui.close | Close, the screen-reader label on every dialog's close button. |
ui.newGame, ui.continue, ui.newGamePlus | New game, Continue and New Game+, on the TitlePage. |
ui.settings, ui.noSettings | Settings and There are no settings., on the title screen, the menu and the settings page. |
ui.credits | Credits, on the title screen and the credits page. |
ui.poweredBy | Powered by, over the IFLoom name on the default splash card. |
ui.visitIfloom | Visit ifloom.dev, the default splash card's link to the IFLoom site. |
ui.skipSplash | Skip, the button that skips the splash cards. |
ui.titlePage | Title screen, the menu's button back to the title screen. |
ui.replayVoice | Hear it again, the button after a spoken line with a voice. |
ui.zoom | Enlarge, the button around a picture that opens the viewer, and the viewer's title when the picture has no alt text. |
ui.closeViewer | Close, the MediaViewer's close button. |
ui.menu | Menu, the title of the dialog Escape opens. |
ui.openSidebar | Open the side panel, the drawer button on a phone. |
ui.changes | What changed, the screen-reader label on Changes. |
ui.timeLeft | Time left, the label on a minigame's countdown bar. |
ui.timerLeft | {seconds}s, the seconds left on a Countdown. |
ui.diceNeeds | needs {against}, a roll's target on a DiceRoll. |
ui.dicePassed, ui.diceFailed | Passed and Failed, a roll's outcome. |
ui.diceCriticalMax, ui.diceCriticalMin | Critical! and Fumble, a critical roll. |
ui.diceRolled | Rolled {dice}: {faces}, total {total}., what a screen reader hears for a roll. |
ui.diceRolledPassed, ui.diceRolledFailed | Needed {against}: passed. and Needed {against}: failed., after it. |
ui.diceDismiss | Dismiss the roll, the DiceOverlay's close button. |
ui.notices, ui.unread | Notices and Unread, the notice strip's count and its screen-reader label. |
ui.noticesMore, ui.noticesFewer | +{n} more and Show fewer, the button that shows or hides older notices on a phone. |
The clock prints view.when, which a plugin writes, so the calendar's own strings reword it. Titles, prose, labels, reasons and price notes come from the game's content, the engine's strings and the plugins' strings. UiStringKey is one of this table's keys without the ui. prefix.
Caveats
mountGameandGameShellplay the game's sound themselves, so auseAudiobeside them takes over from theirs rather than playing twice.- A new
plugins,panelsorcomponentsobject on each render renders every component under the provider again, so define them once outside a component.
Last updated on