Early development: IFLoom can change until 1.0. What's stable
IFLoom
React UI

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/react holds mountGame, GameShell, the layout's regions, every game component and hook, the options that restyle them, and checkUiHints. Its exports are in @ifloom/react.
  • @ifloom/react/kit holds 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, GameShell and 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 ui hints and inline pieces the React UI reads from content, and checkUiHints.
  • React UI parts lists every data-part the 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.

FieldMeaning
rootThe element to render into, or a selector for it. '#root' if left out.
startThe 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.
runnerThe 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:

FieldMeaning
layoutThe 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.
pagesThe 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.
audioThe 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',
  },
}
KeyMeaning
ui.statsStats, the stats section's title and the stats panel's.
ui.statLevelLevel {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.allSeenMark all seen, the button in the NoticeStrip's dialog.
ui.backBack, the button on the settings, load and credits pages.
ui.dismissDismiss, the label on each notice's dismiss button.
ui.undo, ui.redoUndo and Redo, the GameMenu's buttons.
ui.saves, ui.save, ui.saveNew, ui.loadSaves, Save, Save in a new slot and Load, in the SavesMenu.
ui.removeDelete, the button that deletes a save or removes a mod.
ui.exportSave, ui.importSaveExport and Import a save file, in the saves menu and on the load page.
ui.noSaves, ui.brokenSaveNo saves yet. and A save that can’t be read, in the saves menu and on the load page.
ui.missingPacksThis save was made with mods this game doesn’t have: {packs}.
ui.missingPluginsThis save was made with plugins this game doesn’t have: {plugins}.
ui.backlog, ui.backlogEmpty, ui.choseBacklog, Nothing yet. and You chose: {label}, in the Backlog.
ui.enterEnter, the button on an input.
ui.mods, ui.addMod, ui.noModsMods, Add a mod file and No mods added., in the ModManager.
ui.modOn, ui.modOffOn and Off, a mod's state button.
ui.applyMods, ui.modsChangedRestart with these mods and Changes to mods take effect when the game restarts.
ui.modsHintMods 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.closeClose, the screen-reader label on every dialog's close button.
ui.newGame, ui.continue, ui.newGamePlusNew game, Continue and New Game+, on the TitlePage.
ui.settings, ui.noSettingsSettings and There are no settings., on the title screen, the menu and the settings page.
ui.creditsCredits, on the title screen and the credits page.
ui.poweredByPowered by, over the IFLoom name on the default splash card.
ui.visitIfloomVisit ifloom.dev, the default splash card's link to the IFLoom site.
ui.skipSplashSkip, the button that skips the splash cards.
ui.titlePageTitle screen, the menu's button back to the title screen.
ui.replayVoiceHear it again, the button after a spoken line with a voice.
ui.zoomEnlarge, the button around a picture that opens the viewer, and the viewer's title when the picture has no alt text.
ui.closeViewerClose, the MediaViewer's close button.
ui.menuMenu, the title of the dialog Escape opens.
ui.openSidebarOpen the side panel, the drawer button on a phone.
ui.changesWhat changed, the screen-reader label on Changes.
ui.timeLeftTime left, the label on a minigame's countdown bar.
ui.timerLeft{seconds}s, the seconds left on a Countdown.
ui.diceNeedsneeds {against}, a roll's target on a DiceRoll.
ui.dicePassed, ui.diceFailedPassed and Failed, a roll's outcome.
ui.diceCriticalMax, ui.diceCriticalMinCritical! and Fumble, a critical roll.
ui.diceRolledRolled {dice}: {faces}, total {total}., what a screen reader hears for a roll.
ui.diceRolledPassed, ui.diceRolledFailedNeeded {against}: passed. and Needed {against}: failed., after it.
ui.diceDismissDismiss the roll, the DiceOverlay's close button.
ui.notices, ui.unreadNotices 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

  • mountGame and GameShell play the game's sound themselves, so a useAudio beside them takes over from theirs rather than playing twice.
  • A new plugins, panels or components object on each render renders every component under the provider again, so define them once outside a component.

Last updated on

On this page