The Core package provides various helper functions for common tasks in the Thorium Web Reader.
- Breakpoints Management
- Focus Utilities
- Platform Detection
- Progression Formatting
- Props to CSS Variables
function makeBreakpointsMap<T>({
defaultValue,
fromEnum,
pref,
disabledValue,
validateKey
}: {
/** Default value to use when no breakpoint matches */
defaultValue: T;
/** Enum to validate values against */
fromEnum: any;
/** Breakpoint-specific overrides */
pref?: Record<string, T> | boolean;
/** Value to use when the feature is disabled */
disabledValue?: T;
/** Key to validate in nested objects */
validateKey?: string;
}): Required<Record<string, T>>;defaultValue(required): TBDfromEnum(required): TBDpref(optional): TBDdisabledValue(optional): TBDvalidateKey(optional): TBD
defaultValue(required): The default value to use when no breakpoint matchesfromEnum(required): The enum to validate values againstpref(optional): Breakpoint-specific overridesdisabledValue(optional): Value to return when the feature is disabledvalidateKey(optional): For nested objects, the key to validate against the enum
- Creates a map with the default value for all breakpoints
- Applies any breakpoint-specific overrides from
pref - Validates values against the provided enum
- Returns a map with all required breakpoints
Handle focus states and interactive elements consistently.
import {
isActiveElement,
isKeyboardTriggered,
isInteractiveElement
} from "@edrlab/thorium-web/core/helpers";
// Check if an element is currently focused
const active = isActiveElement(element);
// Check if focus was triggered by keyboard
const keyboardFocused = isKeyboardTriggered(element);
// Check if an element is interactive
const interactive = isInteractiveElement(element);Detect the current platform for platform-specific behaviors. Uses the modern navigator.userAgentData API when available, with fallback to navigator.platform.
import {
getPlatform,
isMacish,
isIpadOS,
isIOSish
} from "@edrlab/thorium-web/core/helpers";
// Returns platform name (e.g., "macos", "windows", "android", "linux")
const platform = getPlatform();
// Check for Mac-like platforms (Mac, iPhone, iPad, etc.)
const isMac = isMacish();
// Check for iPadOS in desktop mode
const isIpadDesktop = isIpadOS();
// Check for iOS-like platforms (iPhone, iPad, iPod)
const isIOS = isIOSish();- Uses
navigator.userAgentDatawhen available (modern browsers) - Falls back to
navigator.platformfor compatibility - Handles special cases like Android and iPadOS detection
Handle reading progression formats and calculations.
import {
getSupportedProgressionFormats,
canRenderProgressionFormat,
getBestMatchingProgressionFormat,
ThProgressionFormat
} from "@edrlab/thorium-web/core/helpers";
import { useTimeline } from "@edrlab/thorium-web/core/hooks";
const timeline = useTimeline({
…
});
// Get all supported formats for the current timeline
const supportedFormats = getSupportedProgressionFormats(timeline);
// Check if a specific format can be rendered
const canRender = canRenderProgressionFormat(
ThProgressionFormat.positionsPercentOfTotal,
supportedFormats
);
// Get the best matching format from preferred formats
const bestFormat = getBestMatchingProgressionFormat(
[
ThProgressionFormat.positionsLeft,
ThProgressionFormat.positionsPercentOfTotal
],
timeline
);Convert React/JS props to CSS custom properties with options for prefixing and excluding specific properties.
import { propsToCSSVars } from "@edrlab/thorium-web/core/helpers";
// Basic usage with prefix
const cssVars = propsToCSSVars({
color: "red",
size: "16px",
spacing: {
top: "10px",
bottom: "20px"
}
}, { prefix: "th" });
// Returns: {
// "--th-color": "red",
// "--th-size": "16px",
// "--th-spacing-top": "10px",
// "--th-spacing-bottom": "20px"
// }
// With exclude option
const withExclude = propsToCSSVars({
color: "blue",
size: "18px",
spacing: {
top: "12px",
bottom: "24px"
}
}, {
prefix: "app",
exclude: ["size"] // Exclude the "size" property
});
// Returns: {
// "--app-color": "blue",
// "--app-spacing-top": "12px",
// "--app-spacing-bottom": "24px"
// }