Skip to Content

glost-react

React components for rendering GloST documents with ruby annotations and progressive display.

Installation

npm install @glotblocks/glost-react @glotblocks/glost react

Features

  • Ruby annotation rendering for Asian languages (furigana, pinyin, etc.)
  • Progressive display levels (hide/show annotations)
  • Extensible metadata display
  • TypeScript support

Quick Start

import { GloSTWord, GloSTSentence, GloSTDocument } from '@glotblocks/glost-react'; function MyApp({ document }) { return ( <GloSTDocument document={document}> {document.children.map((paragraph, i) => ( <div key={i}> {paragraph.children.map((sentence, j) => ( <GloSTSentence key={j} sentence={sentence} displayLevel={2}> {sentence.children.map((word, k) => ( <GloSTWord key={k} word={word} /> ))} </GloSTSentence> ))} </div> ))} </GloSTDocument> ); }

Components

GloSTWord

Renders a single word with optional ruby annotation.

import { GloSTWord } from '@glotblocks/glost-react'; <GloSTWord word={word} showRuby={true} rubyKey="romaji" // or "furigana", "ipa", etc. onClick={(word) => console.log(word)} />

Props:

PropTypeDefaultDescription
wordGLOSTWordrequiredThe word node to render
showRubybooleantrueWhether to show ruby annotation
rubyKeystring"romaji"Which transcription to use for ruby
onClick(word) => void-Click handler
classNamestring-Additional CSS class

GloSTSentence

Renders a sentence with its child words.

import { GloSTSentence } from '@glotblocks/glost-react'; <GloSTSentence sentence={sentence} displayLevel={2} transcriptionSystem="paiboon" > {children} </GloSTSentence>

Props:

PropTypeDefaultDescription
sentenceGLOSTSentencerequiredThe sentence node
displayLevelnumber1Display detail level (1-3)
transcriptionSystemstring-Transcription system to use
childrenReactNode-Child word components

GloSTDocument

Context provider for document-level settings.

import { GloSTDocument } from '@glotblocks/glost-react'; <GloSTDocument document={document} defaultDisplayLevel={2} defaultTranscriptionSystem="romaji" > {children} </GloSTDocument>

Display Levels

Control how much information is shown:

LevelDescription
1Text only (no annotations)
2Text with ruby annotations
3Full details (translations, metadata)
// Level 1: 今日 // Level 2: 今日 (with きょう above) // Level 3: 今日 (with きょう above + translation + POS)

Hooks

useGloSTContext

Access document-level settings.

import { useGloSTContext } from '@glotblocks/glost-react/hooks'; function MyComponent() { const { displayLevel, transcriptionSystem } = useGloSTContext(); // ... }

useWordMetadata

Extract metadata from a word.

import { useWordMetadata } from '@glotblocks/glost-react/hooks'; function WordDetails({ word }) { const { frequency, difficulty, pos } = useWordMetadata(word); // ... }

Styling

Components use CSS classes for styling:

.glost-word { /* Word container */ } .glost-word-text { /* Main word text */ } .glost-word-ruby { /* Ruby annotation */ } .glost-sentence { /* Sentence container */ }

Or use the className prop for custom styling:

<GloSTWord word={word} className="my-custom-word" />

TypeScript

Full TypeScript support with exported types:

import type { GloSTWordProps, GloSTSentenceProps, GloSTDocumentProps } from '@glotblocks/glost-react/types';

Example: Thai Learning App

import { GloSTDocument, GloSTSentence, GloSTWord } from '@glotblocks/glost-react'; function ThaiLesson({ document }) { const [displayLevel, setDisplayLevel] = useState(2); return ( <div> <button onClick={() => setDisplayLevel(l => l === 3 ? 1 : l + 1)}> Toggle Details </button> <GloSTDocument document={document} defaultDisplayLevel={displayLevel}> {document.children[0].children.map((sentence, i) => ( <GloSTSentence key={i} sentence={sentence} transcriptionSystem="paiboon" > {sentence.children.map((word, j) => ( <GloSTWord key={j} word={word} onClick={(w) => showDefinition(w)} /> ))} </GloSTSentence> ))} </GloSTDocument> </div> ); }

See Also

Last updated on