Skip to main content
jg-packages

Search

    React hooks

    jg-react

    React hooks for online status, page visibility, breakpoints, viewport proximity, clamped text, new builds, and theme colors, each safe in a server render.

    v0.7.0BrowserESMMIT

    npm

    npm install jg-react

    pnpm

    pnpm add jg-react

    Usage

    ts
    import {
      isBreakpointAtLeast,
      useBreakpoint,
      useDocumentVisibility,
      useIsWindowScrolledPast,
      useOnlineStatus,
    } from 'jg-react';
    
    export const useLayoutState = () => {
      const isOnline = useOnlineStatus(); // the browser's guess, from its network interfaces
      const isVisible = useDocumentVisibility(); // false while the tab is hidden
      const breakpoint = useBreakpoint(); // 'xs' on a phone, then 'sm' to '2xl'
      const isScrolled = useIsWindowScrolledPast(8);
      return { isOnline, isVisible, isDesktop: isBreakpointAtLeast(breakpoint, 'lg'), isScrolled };
    };
    Hook Returns Re-renders when In a server render
    useOnlineStatus() Whether the browser is online It goes online or offline true
    useDocumentVisibility() Whether the page is visible Its tab is hidden or shown true
    useBreakpoint() Tailwind CSS’s breakpoint: xs below sm, up to 2xl The viewport crosses a breakpoint undefined
    useIsWindowScrolledPast(pixels) Whether the window is scrolled further than pixels The answer changes, not every scroll false
    useFormChanges(options) Whether a form’s command differs from the last confirmed one, a submit-time check, and confirmChanges The commands or the baseline change Compared as usual
    useResendCooldown(options) Whether a resend must wait, the seconds left, and startCooldown Every second while cooling down { isCoolingDown: false, remainingSeconds: 0 }
    useCopyFeedback(options?) Whether a copy just succeeded, and copy(write) A copy succeeds, and its feedback ends isCopied: false
    useFileUploadQueue(options) The upload queue’s items, summary, announcement, and controls The queue changes An empty queue
    useResolvedTheme(preference) light or dark, following the system while the preference is system The system’s color scheme changes, for system dark for dark, otherwise light
    useAppliedTheme(preference, options?) The theme it sets as the light or dark class on <html> As useResolvedTheme As useResolvedTheme, without touching the document

    Keep a save button disabled until the form holds a real change, and a resend button waiting after each send:

    ts
    import { useFormChanges, useResendCooldown } from 'jg-react';
    
    const trim = ({ name }: { name: string }) => ({ name: name.trim() });
    
    export const useProfileEditor = (saved: { name: string }, draft: { name: string }) => {
      // a whitespace-only edit isn't a change, and the baseline advances only once a save succeeds
      const { hasChanges, confirmChanges } = useFormChanges({
        confirmedCommand: saved,
        currentCommand: draft,
        normalizeCommand: trim,
      });
      // the deadline survives a reload of the tab, apart from other accounts and actions
      const resend = useResendCooldown({ durationSeconds: 60, cooldownKey: 'account-a:verify-email' });
      return { canSave: hasChanges, confirmChanges, resend };
    };

    The browser hooks read the browser through useSyncExternalStore, so every component sees the same value in the same render, and a server render gets a fixed value instead of failing. The breakpoints are Tailwind CSS’s defaults, in rem, read through media queries, so they follow the browser’s font size as CSS does. Prefer CSS for layout, and useBreakpoint for what CSS can’t do, such as rendering a different component.

    Copy feedback

    useCopyFeedback shows that a copy worked, for a moment. copy takes the function that writes, such as jg-clipboard’s copyText, and calls it at once, before awaiting anything, so the write stays inside the click that browsers require for clipboard access:

    ts
    import { copyText } from 'jg-clipboard';
    import { useCopyFeedback } from 'jg-react';
    
    const { isCopied, copy } = useCopyFeedback({ durationSeconds: 1.4, onError: showCopyError });
    const onClick = () => void copy(() => copyText(inviteLink)); // resolves to whether it worked

    isCopied stays true for durationSeconds, DEFAULT_COPY_FEEDBACK_SECONDS (1.4) by default. A new copy clears it while it runs, so an earlier copy’s state never claims the new one worked, and restarts it once the new one succeeds. A copy while another runs is ignored and resolves to false. A failed one resolves to false and calls the latest onError; copy never rejects. After unmount, a copy that settles changes nothing and calls nothing. A duration that isn’t positive and finite throws INVALID_COPY_FEEDBACK_DURATION.

    Dialog sessions

    createDialogSession keeps one dialog’s state outside React, so any code can open it and await what the user chose, while one host component renders it with useDialogSession:

    ts
    import { createDialogSession, useDialogSession } from 'jg-react';
    import { useDialogNavigationDismissal } from 'jg-react-router';
    
    export const confirmDialog = createDialogSession<{ title: string }, boolean>();
    
    // anywhere, such as a delete button's handler
    const isConfirmed = await confirmDialog.open({ title: 'Delete the project?' }); // true, false, or undefined
    
    // in the host component
    const session = useDialogSession(confirmDialog); // the open session, or null
    useDialogNavigationDismissal({
      getSession: confirmDialog.getSnapshot, // a navigation closes the dialog
      onDismiss: () => confirmDialog.close(),
    });
    const confirm = () => confirmDialog.close(true, session?.id);

    At most one session is open. Opening another resolves the open one’s caller with undefined, or, with whileOpen: 'keep', resolves the new call with undefined, so no caller waits forever, and no promise rejects. A close that names its session’s id does nothing once that session ended, so a late click on a closing dialog can’t close a newer one. One frame after a close, focus returns to the element the session opened from, the focused one by default, when it’s still in the page, enabled, and not inert, no other session opened, and focus is lost on the body; a session that replaces another returns focus where that one would have. A server render reads no open session.

    File uploads

    useFileUploadQueue runs a file upload queue without rendering anything, for a drop zone or a file input in the app’s own markup:

    ts
    import { createFileUploadAcceptValue, useFileUploadQueue } from 'jg-react';
    
    const queue = useFileUploadQueue({
      acceptedFileTypes: ['image/png', 'image/jpeg', '.heic'],
      maxFileBytes: 10 * 1024 * 1024,
      maxFiles: 4,
      isPreviewEnabled: true,
      uploadFile: (file, { signal, reportProgress, uploadId }) =>
        uploadToStorage(file, { signal, onProgress: reportProgress, idempotencyKey: uploadId }),
    });
    // queue.addFiles(input.files), queue.items, queue.summary, queue.announcement,
    // queue.cancelUpload(id), queue.retryUpload(id), queue.removeFile(id), queue.reset()
    const accept = createFileUploadAcceptValue(queue.policy.acceptedFileTypes);
    • Checks: each selection is checked file by file, in a fixed order: empty, type, size, duplicate, count, and total size, with existingFiles counted toward the limits. A validateFile function runs next, such as for an image’s dimensions, and the built-in checks run again after it, since the queue may have changed meanwhile. Selections are checked one at a time.
    • Uploads: admitted files upload first in, first out, up to maxConcurrentUploads at once, 2 by default, or wait for startUploads without autoUpload. Progress is coalesced per frame and never goes backwards. At 100%, a file is processing: it waits for its result, can’t be canceled, and survives removal and reset.
    • Cancel and retry: a canceled upload is aborted through its signal, and its late result is ignored, but its slot frees only when its promise settles. A retry keeps the item’s uploadId and starts the next attemptNumber.
    • One file: with maxFiles 1, the default, a new file replaces the current one only once it passes its checks, so a rejected replacement leaves the current file alone.
    • Cleanup: preview object URLs are revoked on remove, replace, reset, and unmount, and an error a callback throws goes to reportError without stopping the queue. Shown again after an <Activity> hid it, files whose work stopped are canceled.
    • Text: every message and announcement comes from DEFAULT_FILE_UPLOAD_MESSAGES, which messages overrides, and getErrorFeedback decides what a failed upload shows, messages.uploadFailed by default.

    A policy that isn’t valid, such as a maxFileBytes that isn’t a positive integer, throws INVALID_FILE_UPLOAD_POLICY, with a message for each field. resolveFileUploadPolicy, validateFileUploadCandidate, isFileUploadItemCancelable, and hasDraggedFiles work on their own too.

    Light and dark themes

    createThemeInitScript builds an inline script that applies the stored preference before the page paints, so a server render never flashes the wrong theme, and useAppliedTheme keeps the class on <html> in step once React runs:

    tsx
    import { createThemeInitScript, useAppliedTheme } from 'jg-react';
    
    // jg-storage's createLocalStore('preferences') keeps { theme } under this key
    const script = createThemeInitScript({ storageKey: 'jg-storage:preferences', field: 'theme' });
    
    export const ThemeScript = ({ nonce }: { nonce: string }) => (
      <script
        nonce={nonce}
        dangerouslySetInnerHTML={{ __html: script }}
      />
    );
    
    export const ThemeRoot = ({ preference }: { preference: 'light' | 'dark' | 'system' }) => {
      useAppliedTheme(preference); // turns transitions off while the class changes
      return null;
    };

    Set color-scheme per class in your stylesheet, such as :root.dark { color-scheme: dark; }, so form controls and scrollbars follow the theme.

    The preference and theme types, IThemePreference and IResolvedTheme, with THEME_PREFERENCES, in the order ['system', 'light', 'dark'], and resolveThemePreference, come from jg-site, which jg-react depends on, so a site and an app share one definition.

    A component far from the one applying the theme, such as a chart that picks its colors in script, reads the theme the page shows with useDocumentTheme, without the user’s preference: <html>’s dark class, then light, then the system’s color scheme. It re-renders only when that theme changes, not for another class, and reads light on a server and while hydrating, as the init script’s fallback does:

    ts
    import { useDocumentTheme } from 'jg-react';
    
    const lineColor = useDocumentTheme() === 'dark' ? '#93c5fd' : '#1d4ed8';

    Searching as the user types

    useDebouncedSearch searches once the trimmed query has stayed the same for 0.3 seconds, aborting the previous search when the query changes, the search is disabled, or the component unmounts:

    ts
    import { useDebouncedSearch } from 'jg-react';
    
    const { results, isSearching, error, retry } = useDebouncedSearch({
      query,
      enabled: isSearchOpen, // optional
      search: async (text, signal) => {
        const response = await fetch(`/api/articles?q=${encodeURIComponent(text)}`, { signal });
        return (await response.json()) as IArticle[];
      },
    });

    A search that settles after its query changed is ignored, even when search doesn’t stop on the signal. The previous results stay while the next search runs, and an empty query or a disabled search clears everything. retry, such as for a Retry button after error, searches the current query again. A new search function on each render doesn’t restart the wait.

    Table of contents

    useActiveHeading follows which heading the reader is at: the one the URL’s hash names, on mount and on every hashchange, then, as the page scrolls, the last heading whose top passed topOffsetPx, the height of a sticky header, or the first one before any has:

    tsx
    import { useActiveHeading } from 'jg-react';
    
    const { activeHeadingId, setActiveHeadingId } = useActiveHeading({ headingIds, topOffsetPx: 80 });
    // <a href={`#${id}`} aria-current={activeHeadingId === id ? 'location' : undefined}
    //    onClick={() => setActiveHeadingId(id)}>

    Scrolling is followed with an IntersectionObserver, and only the hash where there’s none. A new array with the same ids keeps the observer. A top offset that isn’t a finite number of pixels of at least 0 throws INVALID_ACTIVE_HEADING_OFFSET.

    Element width

    useElementWidth turns an element’s width into the value a component needs, such as how many columns fit, and re-renders only when that value changes, not on every pixel:

    ts
    import { useElementWidth } from 'jg-react';
    
    const { ref, value: columns } = useElementWidth((width) => Math.max(1, Math.floor(width / 280)));
    // <ul ref={ref}>, laid out in `columns ?? 1` columns

    It measures the element’s border box as soon as ref attaches it, before the browser paints, then follows it with a ResizeObserver, or the window’s resize event where there’s none. A width of 0, such as while the element is hidden with display: none, keeps the last value. Attaching another element measures that one, and detaching resets the value to undefined, as on a server. Return a primitive from the function, so an unchanged result never re-renders.

    Near the viewport

    useNearViewport tells when an element is within a margin of the viewport, such as a sentinel at the end of a list that loads the next page before the reader gets there:

    ts
    import { useNearViewport } from 'jg-react';
    
    const { ref, isNear } = useNearViewport({ rootMargin: '0px 0px 512px 0px' });
    // <div ref={ref} /> after the last row; load the next page while `isNear`

    It reads false with no element attached, on a server, and in a browser without IntersectionObserver. A new margin starts a new observer.

    Clamped text

    useIsTextClamped tells whether a text runs past the lines its CSS line-clamp shows, so a Show all control appears only on text the clamp cuts off:

    ts
    import { useIsTextClamped } from 'jg-react';
    
    const { ref, isClamped } = useIsTextClamped({ lines: 3, content: message });
    // <p ref={ref} className={isExpanded ? '' : 'line-clamp-3'}>, with Show all while `isClamped`

    It compares the text’s full height with those lines, so the answer holds whether the text shows clamped or in full, and a row that opens expanded still offers Show less. It measures again before the browser paints when content changes, and whenever the box resizes. Lines that aren’t a positive safe integer throw INVALID_CLAMPED_LINES.

    The current time

    useCurrentTime keeps the time for labels that read relative to it, moving it on once an interval, a minute by default, so “5 minutes ago” changes without a re-render every second:

    ts
    import { useCurrentTime } from 'jg-react';
    import { formatRelativeTime } from 'jg-utilities';
    
    const label = formatRelativeTime(observedAt, { now: useCurrentTime() });

    An interval that isn’t a positive, finite number of seconds throws INVALID_CURRENT_TIME_INTERVAL.

    The browser’s theme color

    useThemeColorMeta keeps every <meta name="theme-color"> tag, which colors the browser’s chrome and an installed app’s title bar, on the color of the theme the page shows:

    ts
    import { useThemeColorMeta } from 'jg-react';
    
    useThemeColorMeta({ light: '#fefdfb', dark: '#1e2939' });

    It follows <html>’s theme as useDocumentTheme reads it, so a theme the user picks in the app wins over the system’s. Leave media queries off those tags, or start them with the system’s colors.

    New builds

    useNewBuildAvailable tells when the server serves a newer build of the app than the open page, so an old tab can offer a reload:

    ts
    import { useNewBuildAvailable } from 'jg-react';
    
    const { isNewBuildAvailable, check } = useNewBuildAvailable();
    // show a Reload banner while `isNewBuildAvailable`; call `check()` when the server is back

    It reads the entry page, / by default, without the cache and without following redirects, and compares the entry script it loads from /assets/ with the open page’s, each time the page becomes visible again and when check asks. A lazy chunk that’s gone after a deploy, which Vite reports with vite:preloadError, counts as a new build while the browser is online; the hook prevents that event’s default, so the import resolves with no module instead of rejecting, which jg-react-router’s loadRouteModule treats as code that couldn’t load. A read that fails, is redirected or isn’t HTML, such as a proxy’s sign-in page, says nothing, and the page never reloads on its own.

    Generated from TSDoc

    API reference

    Every export of [email protected], 89 in all, with its signature, its errors, and an example.

    Functions

    createDialogSession

    function

    Creates one dialog's sessions, outside React, so any code can open the dialog and await its result, such as a confirmation, while one host component renders it. At most one session is open: opening another resolves the open one's caller with undefined (whileOpen: 'replace', the default), or resolves the new call with undefined ('keep'), so no caller waits forever. A promise never rejects. A close can name the session's id, so a stale close, such as from a dialog that already closed, does nothing. One frame after a close, focus returns to the element the session was opened from, when it's still in the page, enabled, and not inert, no other session opened, and focus is lost on the body. useDialogSession reads the open session.

    Signature
    createDialogSession<TOptions, TResult = boolean>(
      options?: IDialogSessionOptions,
    ): IDialogSession<TOptions, TResult>

    Parameters

    options?IDialogSessionOptions

    What opening does while a session is open, and whether focus returns on close.

    Defaults to {}.

    Returns

    IDialogSession<TOptions, TResult>

    The session's functions.

    Example
    import { createDialogSession } from 'jg-react';
    
    export const confirmDialog = createDialogSession<{ title: string }, boolean>();
    
    // anywhere, such as a delete button's handler
    const isConfirmed = await confirmDialog.open({ title: 'Delete the project?' });
    if (isConfirmed === true) {
      // delete it
    }

    createFileUploadAcceptValue

    function

    Builds a file input's accept attribute from a policy's accepted types.

    Signature
    createFileUploadAcceptValue(
      acceptedFileTypes: readonly string[],
    ): string | undefined

    Parameters

    acceptedFileTypesreadonly string[]

    The policy's accepted types.

    Returns

    string | undefined

    The attribute's value, or undefined when every type is accepted.

    Example
    import { createFileUploadAcceptValue } from 'jg-react';
    
    createFileUploadAcceptValue(['image/png', '.jpg']); // 'image/png,.jpg'

    createThemeInitScript

    function

    Builds the inline script that applies the stored theme before the page paints, so a server render doesn't flash the wrong theme. It reads the preference from localStorage, as JSON or as a raw string, from a field of a stored object when field is given, falls back to defaultPreference when it's missing or invalid, and sets the light or dark class on <html>. Storage that can't be read and a browser without matchMedia never make it throw.

    Render it at the top of <head>, with your CSP nonce, as <script nonce={nonce} dangerouslySetInnerHTML={{ __html: script }} />. Its output is the same for the same options, so a hash-based CSP works too. A jg-storage store's key is jg-storage:<id>. Set color-scheme per class in your stylesheet.

    Signature
    createThemeInitScript(options: IThemeInitScriptOptions): string

    Parameters

    optionsIThemeInitScriptOptions

    The localStorage key, and optionally the field and the default preference.

    Returns

    string

    The script's source.

    Throws

    • INVALID_THEME_SCRIPT_OPTIONSThe theme script needs a non-empty storage key, a non-empty field when one is given, and a valid default preference.
    Example
    import { createThemeInitScript } from 'jg-react';
    
    const script = createThemeInitScript({ storageKey: 'jg-storage:preferences', field: 'theme' });

    hasDraggedFiles

    function

    Checks whether a drag carries files, so a drop zone can react to it.

    Signature
    hasDraggedFiles(dragTypes: readonly string[] | DOMStringList): boolean

    Parameters

    dragTypesreadonly string[] | DOMStringList

    The drag's types, such as event.dataTransfer.types.

    Returns

    boolean

    Whether it carries files.

    Example
    import { hasDraggedFiles } from 'jg-react';
    
    const onDragOver = (event: DragEvent) => {
      if (event.dataTransfer !== null && hasDraggedFiles(event.dataTransfer.types)) {
        event.preventDefault();
      }
    };

    isBreakpointAtLeast

    function

    Checks whether a breakpoint is at least as wide as another, such as to show a sidebar from md up. An unknown breakpoint, as in a server render, is never wide enough.

    Signature
    isBreakpointAtLeast(
      breakpoint: IBreakpoint | undefined,
      minimum: IBreakpoint,
    ): boolean

    Parameters

    breakpointIBreakpoint | undefined

    The current breakpoint, from useBreakpoint.

    minimumIBreakpoint

    The narrowest breakpoint that counts.

    Returns

    boolean

    Whether the breakpoint reaches the minimum.

    Example
    import { isBreakpointAtLeast } from 'jg-react';
    
    isBreakpointAtLeast('lg', 'md'); // => true
    isBreakpointAtLeast('xs', 'sm'); // => false
    isBreakpointAtLeast(undefined, 'xs'); // => false

    isFileUploadItemCancelable

    function

    Checks whether an item can still be canceled: it's validating, queued, or sending its bytes. Once every byte is sent, it's processing, and it can't.

    Signature
    isFileUploadItemCancelable<TResult>(item: IFileUploadItem<TResult>): boolean

    Parameters

    itemIFileUploadItem<TResult>

    The item.

    Returns

    boolean

    Whether it can be canceled.

    Example
    import { isFileUploadItemCancelable } from 'jg-react';
    
    declare const item: Parameters<typeof isFileUploadItemCancelable>[0];
    
    const canCancel = isFileUploadItemCancelable(item);

    resolveFileUploadPolicy

    function

    Checks and normalizes an upload policy: accepted types trimmed, lowercased, and without repeats, the defaults filled in, concurrency capped at maxFiles, and single-file replacement only for one file.

    Signature
    resolveFileUploadPolicy(options: IFileUploadPolicyOptions): IFileUploadPolicy

    Parameters

    optionsIFileUploadPolicyOptions

    The policy's fields.

    Returns

    IFileUploadPolicy

    The policy.

    Throws

    • INVALID_FILE_UPLOAD_POLICYThe accepted file types must be an array.
    • INVALID_FILE_UPLOAD_POLICYEach accepted file type must be a string.
    • INVALID_FILE_UPLOAD_POLICYEach accepted file type must be a file extension, such as .png, or a MIME type, such as image/png.
    • INVALID_FILE_UPLOAD_POLICYThe maximum file size must be a positive integer of bytes.
    • INVALID_FILE_UPLOAD_POLICYThe maximum file count must be a positive integer.
    • INVALID_FILE_UPLOAD_POLICYThe maximum concurrent uploads must be a positive integer.
    • INVALID_FILE_UPLOAD_POLICYThe maximum total size must be a positive integer of bytes, when given.
    Example
    import { resolveFileUploadPolicy } from 'jg-react';
    
    resolveFileUploadPolicy({ acceptedFileTypes: [' IMAGE/PNG '], maxFileBytes: 1_048_576 });
    // { acceptedFileTypes: ['image/png'], maxFiles: 1, maxConcurrentUploads: 1, ... }

    useActiveHeading

    function

    Follows which heading the reader is at, for a table of contents that highlights it: the heading the URL's hash names, on mount and on every hashchange, and then, as the page scrolls, the last heading whose top has passed topOffsetPx, the height of a sticky header, or the first heading before any has. Scrolling is followed with an IntersectionObserver, and only the hash without one. The ids are compared by value, so a new array with the same ids doesn't restart anything, and a heading missing from the page is skipped.

    Signature
    useActiveHeading(options: IUseActiveHeadingOptions): IUseActiveHeadingResult

    Parameters

    optionsIUseActiveHeadingOptions

    The headings' ids, in page order, and the top offset in pixels.

    Returns

    IUseActiveHeadingResult

    The active heading's id, and the function that sets it, such as on a link's click.

    Throws

    • INVALID_ACTIVE_HEADING_OFFSETThe active heading's top offset must be a finite number of pixels of at least 0.
    Example
    import { useActiveHeading } from 'jg-react';
    
    export const useTableOfContents = (headingIds: readonly string[]) =>
      useActiveHeading({ headingIds, topOffsetPx: 80 });

    useAppliedTheme

    function

    Applies a theme preference to the page: the light or dark class on <html>, following the system's color scheme while the preference is system. The class changes before the browser paints, with transitions off by default, so colors switch at once. Pair it with createThemeInitScript, so the page shows the right theme before React loads, and set color-scheme per class in your stylesheet.

    Signature
    useAppliedTheme(
      preference: IThemePreference,
      options?: IAppliedThemeOptions,
    ): IResolvedTheme

    Parameters

    preferenceIThemePreference

    The user's preference, such as from a settings store.

    options?IAppliedThemeOptions

    Whether to turn transitions off while the class changes. Defaults to true.

    Defaults to {}.

    Returns

    IResolvedTheme

    The theme applied, light or dark.

    Example
    import { useAppliedTheme } from 'jg-react';
    
    export const ThemeRoot = ({ preference }: { preference: 'light' | 'dark' | 'system' }) => {
      useAppliedTheme(preference);
      return null;
    };

    useBreakpoint

    function

    Reports the viewport's Tailwind CSS breakpoint, re-rendering only when the viewport crosses one: xs below sm, such as on a phone, then sm, md, lg, xl, and 2xl. A server render, which has no viewport, reports undefined, so prefer CSS for layout and this for what CSS can't do.

    Signature
    useBreakpoint(): IBreakpoint | undefined

    Returns

    IBreakpoint | undefined

    The breakpoint, or undefined in a server render.

    Example
    import { isBreakpointAtLeast, useBreakpoint } from 'jg-react';
    
    export const useIsDesktopLayout = (): boolean => isBreakpointAtLeast(useBreakpoint(), 'lg');

    useCopyFeedback

    function

    Shows that a copy worked, for a moment, such as a "Copied" label on a copy button. copy calls the write function at once, before it awaits anything, so a clipboard write still runs inside the click that browsers require; then isCopied stays true for durationSeconds. A new copy clears an earlier one's copied state while it runs, a copy while another runs is ignored, and a failed one calls onError and leaves isCopied false. copy never rejects, and after unmount nothing changes and nothing is called.

    Signature
    useCopyFeedback(options?: IUseCopyFeedbackOptions): IUseCopyFeedbackResult

    Parameters

    options?IUseCopyFeedbackOptions

    How long the copied state lasts, and what receives a failed copy's error.

    Defaults to {}.

    Returns

    IUseCopyFeedbackResult

    Whether a copy just succeeded, and copy, which keeps its identity.

    Throws

    • INVALID_COPY_FEEDBACK_DURATIONThe copy feedback duration must be a positive, finite number of seconds.
    Example
    import { copyText } from 'jg-clipboard';
    import { useCopyFeedback } from 'jg-react';
    
    export const useCopyLink = (url: string) => {
      const { isCopied, copy } = useCopyFeedback();
      return { isCopied, copyLink: () => copy(() => copyText(url)) };
    };

    useCurrentTime

    function

    Keeps the current time for labels that read relative to it, such as "5 minutes ago", moving it on once an interval, a minute by default, so they change without re-rendering every second. The first render reads the time it renders at, which a server render reads too.

    Signature
    useCurrentTime(options?: IUseCurrentTimeOptions): number

    Parameters

    options?IUseCurrentTimeOptions

    How often the time moves on, in seconds.

    Defaults to {}.

    Returns

    number

    The current time, in Unix milliseconds.

    Throws

    • INVALID_CURRENT_TIME_INTERVALThe current time's interval must be a positive, finite number of seconds.
    Example
    import { useCurrentTime } from 'jg-react';
    import { formatRelativeTime } from 'jg-utilities';
    
    export const useObservedLabel = (observedAt: number) =>
      `Observed ${formatRelativeTime(observedAt, { now: useCurrentTime() })}`;

    useDebouncedSearch

    function

    Searches as the user types: once the trimmed query has stayed the same for 0.3 seconds, it calls search with it and a signal, and keeps the results. A new query, disabling the search, and unmounting abort the search in flight, and a search that settles after its query changed is ignored, even when search doesn't stop on the signal. The previous results stay while the next search runs, and an empty query or a disabled search clears everything.

    Signature
    useDebouncedSearch<TResult>(
      options: IUseDebouncedSearchOptions<TResult>,
    ): IUseDebouncedSearchResult<TResult>

    Parameters

    optionsIUseDebouncedSearchOptions<TResult>

    The query, whether to search, and the function that searches.

    Returns

    IUseDebouncedSearchResult<TResult>

    The results, whether a search is waiting or running, its error, and retry.

    Example
    import { useDebouncedSearch } from 'jg-react';
    
    export const useArticleSearch = (query: string) =>
      useDebouncedSearch({
        query,
        search: async (text, signal) => {
          const response = await fetch(`/api/articles?q=${encodeURIComponent(text)}`, { signal });
          return (await response.json()) as { id: string; title: string }[];
        },
      });

    useDialogSession

    function

    Reads a dialog session's open session, re-rendering when one opens or closes, such as in the component that renders the dialog. A server render reads null.

    Signature
    useDialogSession<TOptions, TResult>(
      session: IDialogSession<TOptions, TResult>,
    ): IDialogSessionSnapshot<TOptions> | null

    Parameters

    sessionIDialogSession<TOptions, TResult>

    The dialog's sessions, from createDialogSession.

    Returns

    IDialogSessionSnapshot<TOptions> | null

    The open session, the same object while it lasts, or null.

    Example
    import { createDialogSession, useDialogSession } from 'jg-react';
    
    export const confirmDialog = createDialogSession<{ title: string }, boolean>();
    
    // in the component that renders the dialog
    export const useConfirmDialog = () => {
      const session = useDialogSession(confirmDialog);
      return {
        isOpen: session !== null,
        title: session?.options.title ?? '',
        // the id keeps a click on a dialog that already closed from closing a newer one
        confirm: () => confirmDialog.close(true, session?.id),
        cancel: () => confirmDialog.close(false, session?.id),
      };
    };

    useDocumentTheme

    function

    Reads the theme the page shows from <html>'s class, as useAppliedTheme and createThemeInitScript write it: dark wins, then light, then the system's color scheme. It re-renders only when that theme changes, not for another class, so a component far from the one applying the theme, such as a chart, can follow it without the user's preference. A server render, and so the hydration render, reads light.

    Signature
    useDocumentTheme(): IResolvedTheme

    Returns

    IResolvedTheme

    light or dark.

    Example
    import { useDocumentTheme } from 'jg-react';
    
    export const useChartColors = () =>
      useDocumentTheme() === 'dark' ? { line: '#93c5fd' } : { line: '#1d4ed8' };

    useDocumentVisibility

    function

    Reports whether the page is visible, re-rendering when its tab is hidden or shown again, such as to pause polling while nobody looks. A server render reports visible.

    Signature
    useDocumentVisibility(): boolean

    Returns

    boolean

    Whether the page is visible.

    Example
    import { useEffect } from 'react';
    import { useDocumentVisibility } from 'jg-react';
    
    declare const refreshPrices: () => void;
    
    export const usePricePolling = (): void => {
      const isVisible = useDocumentVisibility();
      useEffect(() => {
        if (!isVisible) {
          return undefined;
        }
        const timer = setInterval(refreshPrices, 30_000);
        return () => clearInterval(timer);
      }, [isVisible]);
    };

    useElementWidth

    function

    Reads an element's width as a value the component needs, such as how many columns fit, and re-renders only when that value changes, not on every pixel. It measures the element's border box as soon as it's attached, before the browser paints, then follows it with a ResizeObserver, or the window's resize event where there's none. A width of 0, such as while the element is hidden with display: none, keeps the last value. Attaching another element measures that one, and detaching resets the value to undefined, as on a server.

    Signature
    useElementWidth<T>(select: (width: number) => T): IUseElementWidthResult<T>

    Parameters

    select(width: number) => T

    Turns a width in pixels into the value, such as a column count.

    Returns

    IUseElementWidthResult<T>

    The ref to attach, and the value.

    Example
    import { useElementWidth } from 'jg-react';
    
    export const useGridColumns = () => useElementWidth((width) => Math.max(1, Math.floor(width / 280)));

    useFileUploadQueue

    function

    Runs a headless file upload queue, for a drop zone or a file input in the app's own markup. Selections are checked one at a time, against the built-in checks, then validateFile, then the built-in checks again, since the queue may have changed meanwhile. Admitted files upload first in, first out, up to maxConcurrentUploads at once, with progress coalesced per frame. At 100%, an upload is processing: it can't be canceled anymore, and it survives removal and reset. A canceled upload is aborted and its late result ignored, but its slot frees only once its promise settles. A retry keeps the item's id. With maxFiles 1, a new file replaces the current one only once it validates. Object URLs are revoked on remove, replace, reset, and unmount, and errors the callbacks throw go to reportError. Shown again after an <Activity> hid it, items whose work stopped are canceled.

    Signature
    useFileUploadQueue<TResult>(
      options: IUseFileUploadQueueOptions<TResult>,
    ): IUseFileUploadQueueResult<TResult>

    Parameters

    optionsIUseFileUploadQueueOptions<TResult>

    The policy, the upload and validation functions, the messages, and the callbacks.

    Returns

    IUseFileUploadQueueResult<TResult>

    The controls, which keep their identity, the items, their summary, the latest announcement for a polite live region, and the policy.

    Throws

    • INVALID_FILE_UPLOAD_POLICYThe accepted file types must be an array.
    • INVALID_FILE_UPLOAD_POLICYEach accepted file type must be a string.
    • INVALID_FILE_UPLOAD_POLICYEach accepted file type must be a file extension, such as .png, or a MIME type, such as image/png.
    • INVALID_FILE_UPLOAD_POLICYThe maximum file size must be a positive integer of bytes.
    • INVALID_FILE_UPLOAD_POLICYThe maximum file count must be a positive integer.
    • INVALID_FILE_UPLOAD_POLICYThe maximum concurrent uploads must be a positive integer.
    • INVALID_FILE_UPLOAD_POLICYThe maximum total size must be a positive integer of bytes, when given.
    Example
    import { useFileUploadQueue } from 'jg-react';
    
    declare const uploadAvatar: (file: File, signal: AbortSignal) => Promise<{ url: string }>;
    
    export const useAvatarUpload = () =>
      useFileUploadQueue({
        acceptedFileTypes: ['image/png', 'image/jpeg'],
        maxFileBytes: 5 * 1024 * 1024,
        isPreviewEnabled: true,
        uploadFile: (file, { signal }) => uploadAvatar(file, signal),
      });

    useFormChanges

    function

    Tells whether a form holds unsaved changes, by comparing the command it edits with the last one the server confirmed, so a save button can stay disabled until something changed. Commands are compared by value, as JSON: a Map or a Set compares as {}, a key whose value is undefined doesn't count, and a BigInt throws UNABLE_TO_SERIALIZE_JSON. Pass normalizeCommand to compare commands the way the server stores them, such as with trimmed text.

    The baseline advances only when confirmedCommand changes by value, or through confirmChanges once a save succeeds, so a failed save keeps the changes. guardSubmission checks again as the form submits, in case the command changed since the last render.

    Signature
    useFormChanges<TCommand extends object, TNormalizedCommand extends object = TCommand>(
      options: IUseFormChangesOptions<TCommand, TNormalizedCommand>,
    ): IUseFormChangesResult<TCommand>

    Parameters

    optionsIUseFormChangesOptions<TCommand, TNormalizedCommand>

    The confirmed command, the current one, and the optional normalizer.

    Returns

    IUseFormChangesResult<TCommand>

    Whether the form holds changes, the submit-time check, and confirmChanges.

    Throws

    • UNABLE_TO_SERIALIZE_JSONif a command holds a value JSON can't represent, such as a BigInt
    Example
    import { useFormChanges } from 'jg-react';
    
    type IProfileCommand = { name: string; bio: string };
    
    const trimProfile = ({ name, bio }: IProfileCommand): IProfileCommand => ({
      name: name.trim(),
      bio: bio.trim(),
    });
    
    export const useProfileForm = (saved: IProfileCommand, draft: IProfileCommand) => {
      const { hasChanges, guardSubmission, confirmChanges } = useFormChanges({
        confirmedCommand: saved,
        currentCommand: draft,
        normalizeCommand: trimProfile,
      });
      return { canSave: hasChanges, guardSubmission, confirmChanges };
    };

    useIsTextClamped

    function

    Measures whether a text runs past the lines its clamp shows, so a Show all control appears only on text the clamp cuts off. It compares the text's full height with those lines, which holds whether the text shows clamped or in full, so a row that opens already expanded still offers Show less. A ResizeObserver measures it again whenever its box changes, such as on a narrower screen, and new content is measured as it renders, before the browser paints.

    Signature
    useIsTextClamped(options: IUseIsTextClampedOptions): IUseIsTextClampedResult

    Parameters

    optionsIUseIsTextClampedOptions

    The lines the clamp shows, and the text.

    Returns

    IUseIsTextClampedResult

    The ref to attach to the clamped element, and whether its text runs past the clamp.

    Throws

    • INVALID_CLAMPED_LINESThe clamped lines must be a positive safe integer.
    Example
    import { useIsTextClamped } from 'jg-react';
    
    export const useMessageClamp = (text: string) => useIsTextClamped({ lines: 3, content: text });

    useIsWindowScrolledPast

    function

    Reports whether the window is scrolled past a number of pixels, such as to shadow a sticky header or show a back-to-top button. It re-renders only when the answer changes, not on every scroll. A server render reports false.

    Signature
    useIsWindowScrolledPast(pixels: number): boolean

    Parameters

    pixelsnumber

    How far down the window must be scrolled, in pixels.

    Returns

    boolean

    Whether it's scrolled further than that.

    Example
    import { useIsWindowScrolledPast } from 'jg-react';
    
    export const useHeaderShadow = (): string =>
      useIsWindowScrolledPast(8) ? 'shadow-md' : 'shadow-none';

    useNearViewport

    function

    Watches whether an element is within a margin of the viewport, such as a sentinel at the end of a list that loads more as it comes into view, with one IntersectionObserver for the element and the margin. A browser without IntersectionObserver, and a server, read it as not near.

    Signature
    useNearViewport(options?: IUseNearViewportOptions): IUseNearViewportResult

    Parameters

    options?IUseNearViewportOptions

    The margin around the viewport.

    Defaults to {}.

    Returns

    IUseNearViewportResult

    The ref to attach, and whether the element is near the viewport.

    Example
    import { useNearViewport } from 'jg-react';
    
    export const useLoadMoreSentinel = () => useNearViewport({ rootMargin: '0px 0px 512px 0px' });

    useNewBuildAvailable

    function

    Tells when the server serves a newer build of the app than the open page, so an old tab can offer a reload instead of missing what a deploy changed. It compares the entry script the entry page loads now with the open page's, each time the page becomes visible again and when check asks, and treats a lazy chunk the open build named that's gone, which Vite reports with vite:preloadError, as a new build, unless the browser is offline. It prevents that event's default, so the import resolves with no module instead of rejecting, which route loaders such as jg-react-router's loadRouteModule treat as code that couldn't load. A read that fails, is redirected or isn't HTML says nothing. The page never reloads on its own.

    Signature
    useNewBuildAvailable(
      options?: IUseNewBuildAvailableOptions,
    ): IUseNewBuildAvailableResult

    Parameters

    options?IUseNewBuildAvailableOptions

    The entry page and the folder of the build's hashed scripts.

    Defaults to {}.

    Returns

    IUseNewBuildAvailableResult

    Whether a newer build is available, and what checks again.

    Example
    import { useNewBuildAvailable } from 'jg-react';
    
    export const useUpdateBanner = () => useNewBuildAvailable().isNewBuildAvailable;

    useOnlineStatus

    function

    Reports whether the browser is online, re-rendering when it goes online or offline. It's the browser's own guess, from its network interfaces, not proof that a server can be reached, so use it to explain a failure, not to skip a request. A server render reports online.

    Signature
    useOnlineStatus(): boolean

    Returns

    boolean

    Whether the browser is online.

    Example
    import { useOnlineStatus } from 'jg-react';
    
    export const OfflineBanner = () => {
      const isOnline = useOnlineStatus();
      return isOnline ? null : 'You are offline. Changes will sync when you reconnect.';
    };

    useResendCooldown

    function

    Makes a resend button, such as for a verification email or a one-time code, wait after each send, with a countdown. Call startCooldown once the server acknowledged a send. With a cooldownKey, the deadline is kept in sessionStorage, so it survives a reload of the tab, and in memory when storage fails. Until the tab sends itself, lastAttemptedAt, when the server says the last send happened, sets the deadline; a time in the future counts as now.

    The first render, on the server and while hydrating, is always { isCoolingDown: false, remainingSeconds: 0 }, and the deadline is read right after mount, before the browser paints. So a server-rendered button looks enabled until hydration, when it can't be clicked anyway.

    Signature
    useResendCooldown(options: IUseResendCooldownOptions): IUseResendCooldownResult

    Parameters

    optionsIUseResendCooldownOptions

    The duration in seconds, and optionally the cooldown's key, the server's last send, and the jg-storage store id.

    Returns

    IUseResendCooldownResult

    Whether it's cooling down, the seconds left, and startCooldown.

    Throws

    • INVALID_COOLDOWN_DURATIONThe cooldown duration must be a positive safe integer of seconds.
    • INVALID_STORE_IDThe store id must hold only letters, digits, underscores, and hyphens.
    Example
    import { useResendCooldown } from 'jg-react';
    
    export const useVerificationResend = (accountId: string, lastSentAt: string | null) =>
      useResendCooldown({
        durationSeconds: 60,
        cooldownKey: `${accountId}:verify-email`,
        lastAttemptedAt: lastSentAt,
      });

    useResolvedTheme

    function

    Resolves a theme preference to the theme a page shows, following the system's color scheme while the preference is system, and re-rendering when it changes. A server render, which can't read the system, gives dark for dark and light otherwise.

    Signature
    useResolvedTheme(preference: IThemePreference): IResolvedTheme

    Parameters

    preferenceIThemePreference

    The user's preference.

    Returns

    IResolvedTheme

    light or dark.

    Example
    import { useResolvedTheme } from 'jg-react';
    
    export const CodeBlock = ({ preference }: { preference: 'light' | 'dark' | 'system' }) => {
      const theme = useResolvedTheme(preference);
      return theme === 'dark' ? 'github-dark' : 'github-light';
    };

    useThemeColorMeta

    function

    Keeps the browser's chrome, and an installed app's title bar, on the theme the page shows: it sets every <meta name="theme-color"> tag to the theme's color whenever <html>'s theme changes, as useAppliedTheme and createThemeInitScript write it. Keep media queries off those tags, or start them with the system's colors, since the page's theme decides from then on.

    Signature
    useThemeColorMeta(colors: IThemeColors): void

    Parameters

    colorsIThemeColors

    The color for each theme.

    Example
    import { useThemeColorMeta } from 'jg-react';
    
    const THEME_COLORS = { light: '#fefdfb', dark: '#1e2939' };
    
    export const useAppThemeColor = (): void => useThemeColorMeta(THEME_COLORS);

    validateFileUploadCandidate

    function

    Checks a file against a policy and the files already admitted, in a fixed order: empty, type, size, duplicate, count, and total size.

    Signature
    validateFileUploadCandidate(
      file: File,
      admittedFiles: readonly File[],
      policy: IFileUploadPolicy,
      messages?: IFileUploadMessages,
    ): IFileUploadValidationIssue | null

    Parameters

    fileFile

    The file.

    admittedFilesreadonly File[]

    The files that count toward the limits.

    policyIFileUploadPolicy

    The policy, from resolveFileUploadPolicy.

    messages?IFileUploadMessages

    The text of the issues. Defaults to DEFAULT_FILE_UPLOAD_MESSAGES.

    Defaults to DEFAULT_FILE_UPLOAD_MESSAGES.

    Returns

    IFileUploadValidationIssue | null

    The first issue, or null when the file is admitted.

    Example
    import { resolveFileUploadPolicy, validateFileUploadCandidate } from 'jg-react';
    
    const policy = resolveFileUploadPolicy({ acceptedFileTypes: ['image/*'], maxFileBytes: 1024 });
    validateFileUploadCandidate(new File([], 'empty.png', { type: 'image/png' }), [], policy);
    // { reason: 'empty-file', message: '"empty.png" is empty and cannot be uploaded.', ... }

    Constants

    DEFAULT_COPY_FEEDBACK_SECONDS

    const

    the seconds the copied state lasts when the options don't set them

    Declaration
    const DEFAULT_COPY_FEEDBACK_SECONDS: 1.4

    DEFAULT_FILE_UPLOAD_MESSAGES

    const

    the queue's text in English, which messages overrides

    Declaration
    const DEFAULT_FILE_UPLOAD_MESSAGES: IFileUploadMessages

    ERRORS

    const

    the codes for every error thrown by the package

    Declaration
    const ERRORS: { INVALID_COOLDOWN_DURATION: "INVALID_COOLDOWN_DURATION"; INVALID_THEME_SCRIPT_OPTIONS: "INVALID_THEME_SCRIPT_OPTIONS"; INVALID_COPY_FEEDBACK_DURATION: "INVALID_COPY_FEEDBACK_DURATION"; INVALID_FILE_UPLOAD_POLICY: "INVALID_FILE_UPLOAD_POLICY"; INVALID_ACTIVE_HEADING_OFFSET: "INVALID_ACTIVE_HEADING_OFFSET"; INVALID_CURRENT_TIME_INTERVAL: "INVALID_CURRENT_TIME_INTERVAL"; INVALID_CLAMPED_LINES: "INVALID_CLAMPED_LINES" }

    Types

    IAppliedThemeOptions

    type

    how useAppliedTheme applies the theme

    Declaration
    type IAppliedThemeOptions = {
        disableTransitionsOnChange?: boolean | undefined;
    };

    Members

    • disableTransitionsOnChangeproperty
      boolean

      turns transitions off while the theme class changes, so colors switch at once. Defaults to true

    IBreakpoint

    type

    Tailwind CSS's breakpoints, from the narrowest: xs is below sm, such as a phone

    Declaration
    type IBreakpoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl';

    IDialogSession

    type

    one dialog's sessions: at most one open at a time, each resolving its caller's promise once

    Declaration
    type IDialogSession<TOptions, TResult> = {
        open: (options: TOptions, init?: IDialogSessionOpenInit) => Promise<TResult | undefined>;
        close: (result?: TResult, id?: number) => boolean;
        getSnapshot: () => IDialogSessionSnapshot<TOptions> | null;
        subscribe: (listener: () => void) => () => void;
    };

    Members

    • openproperty
      (options: TOptions, init?: IDialogSessionOpenInit) => Promise<TResult | undefined>

      opens a session and resolves to the result it closes with, or undefined when it closes without one or another session replaces it; it never rejects

    • closeproperty
      (result?: TResult, id?: number) => boolean

      closes the open session with a result, or only the session with the given id, and returns whether one closed

    • getSnapshotproperty
      () => IDialogSessionSnapshot<TOptions> | null

      the open session, or null

    • subscribeproperty
      (listener: () => void) => () => void

      calls the listener when a session opens or closes, and returns what stops that

    IDialogSessionOpenInit

    type

    how one open call starts its session

    Declaration
    type IDialogSessionOpenInit = {
        returnFocusTo?: HTMLElement | null | undefined;
    };

    Members

    • returnFocusToproperty
      HTMLElement | null

      the element focus returns to once the session closes. Defaults to the element that has focus when the session opens, or, when it replaces one, to that session's element

    IDialogSessionOptions

    type

    how a dialog session behaves

    Declaration
    type IDialogSessionOptions = {
        whileOpen?: 'replace' | 'keep' | undefined;
        restoreFocus?: boolean | undefined;
    };

    Members

    • whileOpenproperty
      "replace" | "keep"

      what open does while a session is open: 'replace', the default, resolves the open session's caller with undefined and opens the new one; 'keep' leaves the open one and resolves the new call with undefined

    • restoreFocusproperty
      boolean

      whether closing returns focus to the element the session was opened from. Defaults to true

    IDialogSessionSnapshot

    type

    an open session: the same object for as long as the session lasts

    Declaration
    type IDialogSessionSnapshot<TOptions> = {
        readonly id: number;
        readonly options: TOptions;
        readonly returnFocusTo: HTMLElement | null;
    };

    Members

    • idproperty
      number

      the session's id, which close can name so a stale close does nothing

    • optionsproperty
      TOptions

      what the dialog shows, as open received it

    • returnFocusToproperty
      HTMLElement | null

      the element focus returns to once the session closes

    IFileUploadCanceledHandler

    type

    receives a file once it's canceled

    Declaration
    type IFileUploadCanceledHandler = (item: IFileUploadCanceledItem) => void;

    IFileUploadCanceledItem

    type

    a file canceled before every byte was sent, which can be retried

    Declaration
    type IFileUploadCanceledItem = IFileUploadItemBase & {
        status: 'canceled';
    };

    IFileUploadControls

    type

    what controls the queue

    Declaration
    type IFileUploadControls = {
        addFiles: (files: Iterable<File>) => void;
        cancelAllUploads: () => void;
        cancelUpload: (uploadId: string) => void;
        removeFile: (uploadId: string) => void;
        reset: () => void;
        retryUpload: (uploadId: string) => void;
        startUploads: () => void;
    };

    Members

    • addFilesproperty
      (files: Iterable<File>) => void

      validates the files and queues the ones admitted, one selection at a time

    • cancelAllUploadsproperty
      () => void

      cancels every file that hasn't finished sending its bytes

    • cancelUploadproperty
      (uploadId: string) => void

      cancels a file that hasn't finished sending its bytes

    • removeFileproperty
      (uploadId: string) => void

      removes a file, canceling it first; a processing file stays

    • resetproperty
      () => void

      removes every file but the processing ones, canceling them first

    • retryUploadproperty
      (uploadId: string) => void

      queues a failed or canceled file again, under the same id

    • startUploadsproperty
      () => void

      starts the queued files, without autoUpload

    IFileUploadErrorDetails

    type

    an error's feedback, with the original error as its cause

    Declaration
    type IFileUploadErrorDetails = IFileUploadErrorFeedback & {
        cause: unknown;
    };

    IFileUploadErrorFeedback

    type

    what the user sees about an error, as getErrorFeedback gives it

    Declaration
    type IFileUploadErrorFeedback = {
        message: string;
        code: string | null;
    };

    Members

    • messageproperty
      string

      the message to show

    • codeproperty
      string | null

      a code the user may quote to support, or null

    IFileUploadFailedItem

    type

    a file whose upload failed, with what to show for its error

    Declaration
    type IFileUploadFailedItem = IFileUploadItemBase & {
        status: 'failed';
        error: IFileUploadErrorDetails;
    };

    IFileUploadFailureHandler

    type

    receives a file once its upload failed

    Declaration
    type IFileUploadFailureHandler = (item: IFileUploadFailedItem) => void;

    IFileUploadHandler

    type

    uploads one file, resolving to what the app keeps, such as the stored file's URL

    Declaration
    type IFileUploadHandler<TResult> = (file: File, context: IFileUploadHandlerContext) => Promise<TResult>;

    IFileUploadHandlerContext

    type

    what an upload attempt receives besides the file

    Declaration
    type IFileUploadHandlerContext = {
        attemptNumber: number;
        reportProgress: (uploadedBytes: number, totalBytes: number) => void;
        signal: AbortSignal;
        uploadId: string;
    };

    Members

    • attemptNumberproperty
      number

      the attempt's number, from 1

    • reportProgressproperty
      (uploadedBytes: number, totalBytes: number) => void

      reports the bytes sent so far; reports are coalesced per frame and never go backwards

    • signalproperty
      AbortSignal

      aborts when the upload is canceled, removed, replaced, or the hook unmounts

    • uploadIdproperty
      string

      the item's id, the same for every attempt, as an idempotency key

    IFileUploadItem

    type

    a file in the queue

    Declaration
    type IFileUploadItem<TResult> = IFileUploadValidatingItem | IFileUploadQueuedItem | IFileUploadUploadingItem | IFileUploadProcessingItem | IFileUploadSucceededItem<TResult> | IFileUploadFailedItem | IFileUploadCanceledItem | IFileUploadRejectedItem;

    IFileUploadItemsChangeHandler

    type

    receives the items and their summary each time the queue changes

    Declaration
    type IFileUploadItemsChangeHandler<TResult> = (items: readonly IFileUploadItem<TResult>[], summary: IFileUploadSummary) => void;

    IFileUploadMessages

    type

    the text the queue shows and announces, which an app can translate or reword

    Declaration
    type IFileUploadMessages = {
        emptyFile: (fileName: string) => string;
        unsupportedFileType: (fileName: string, acceptedFileTypes: readonly string[]) => string;
        fileTooLarge: (fileName: string, maxFileBytes: number, excessBytes: number) => string;
        duplicateFile: (fileName: string) => string;
        fileCountExceeded: (maxFiles: number) => string;
        totalSizeExceeded: (fileName: string, maxTotalBytes: number) => string;
        uploadFailed: string;
        fileIssue: (fileName: string, message: string) => string;
        queued: (fileName: string) => string;
        uploadStarted: (fileName: string) => string;
        uploadComplete: (fileName: string) => string;
        canceled: (fileName: string) => string;
        queuedForRetry: (fileName: string) => string;
        queueCleared: string;
        queueClearedWhileProcessing: string;
    };

    Members

    • emptyFileproperty
      (fileName: string) => string
    • unsupportedFileTypeproperty
      (fileName: string, acceptedFileTypes: readonly string[]) => string
    • fileTooLargeproperty
      (fileName: string, maxFileBytes: number, excessBytes: number) => string
    • duplicateFileproperty
      (fileName: string) => string
    • fileCountExceededproperty
      (maxFiles: number) => string
    • totalSizeExceededproperty
      (fileName: string, maxTotalBytes: number) => string
    • uploadFailedproperty
      string

      the message of a failed upload, or of a validator that threw, unless getErrorFeedback gives one

    • fileIssueproperty
      (fileName: string, message: string) => string

      announcements, for a polite live region

    • queuedproperty
      (fileName: string) => string
    • uploadStartedproperty
      (fileName: string) => string
    • uploadCompleteproperty
      (fileName: string) => string
    • canceledproperty
      (fileName: string) => string
    • queuedForRetryproperty
      (fileName: string) => string
    • queueClearedproperty
      string
    • queueClearedWhileProcessingproperty
      string

    IFileUploadPolicy

    type

    the policy every file is checked against, as resolveFileUploadPolicy normalizes it

    Declaration
    type IFileUploadPolicy = {
        acceptedFileTypes: readonly string[];
        allowDuplicateFiles: boolean;
        autoUpload: boolean;
        maxConcurrentUploads: number;
        maxFileBytes: number;
        maxFiles: number;
        maxTotalBytes: number | null;
        replaceSingleFile: boolean;
    };

    Members

    • acceptedFileTypesproperty
      readonly string[]
    • allowDuplicateFilesproperty
      boolean
    • autoUploadproperty
      boolean
    • maxConcurrentUploadsproperty
      number
    • maxFileBytesproperty
      number
    • maxFilesproperty
      number
    • maxTotalBytesproperty
      number | null
    • replaceSingleFileproperty
      boolean

    IFileUploadPolicyOptions

    type

    what resolveFileUploadPolicy takes

    Declaration
    type IFileUploadPolicyOptions = {
        acceptedFileTypes: readonly string[];
        allowDuplicateFiles?: boolean | undefined;
        autoUpload?: boolean | undefined;
        maxConcurrentUploads?: number | undefined;
        maxFileBytes: number;
        maxFiles?: number | undefined;
        maxTotalBytes?: number | undefined;
        replaceSingleFile?: boolean | undefined;
    };

    Members

    • acceptedFileTypesproperty
      readonly string[]

      file extensions, such as .png, and MIME types, such as image/png or image/*; none accepts every type

    • allowDuplicateFilesproperty
      boolean

      whether a file with the same name, size, type, and date as an admitted one is admitted. Defaults to false

    • autoUploadproperty
      boolean

      whether queued files start uploading at once. Defaults to true

    • maxConcurrentUploadsproperty
      number

      the most uploads running at once. Defaults to 2, and never more than maxFiles

    • maxFileBytesproperty
      number

      the largest file accepted, in bytes

    • maxFilesproperty
      number

      the most files admitted at once, the existing files included. Defaults to 1

    • maxTotalBytesproperty
      number

      the most bytes admitted at once, the existing files included

    • replaceSingleFileproperty
      boolean

      with maxFiles 1, whether a new file replaces the current one, once it validates. Defaults to true

    IFileUploadProcessingItem

    type

    every byte is sent and the upload waits for its result; it can't be canceled anymore

    Declaration
    type IFileUploadProcessingItem = IFileUploadItemBase & {
        status: 'processing';
    };

    IFileUploadProgress

    type

    how much of a file is uploaded; without a report yet, its total is the file's size

    Declaration
    type IFileUploadProgress = {
        hasReportedProgress: boolean;
        percentage: number;
        totalBytes: number;
        uploadedBytes: number;
    };

    Members

    • hasReportedProgressproperty
      boolean
    • percentageproperty
      number
    • totalBytesproperty
      number
    • uploadedBytesproperty
      number

    IFileUploadQueuedItem

    type

    a file admitted, waiting for a slot or for startUploads

    Declaration
    type IFileUploadQueuedItem = IFileUploadItemBase & {
        status: 'queued';
    };

    IFileUploadRejectedHandler

    type

    receives a file the checks refused

    Declaration
    type IFileUploadRejectedHandler = (item: IFileUploadRejectedItem) => void;

    IFileUploadRejectedItem

    type

    a file not admitted, with the check that refused it

    Declaration
    type IFileUploadRejectedItem = IFileUploadItemBase & {
        status: 'rejected';
        validationIssue: IFileUploadValidationIssue;
    };

    IFileUploadStatus

    type

    where a file is in its life in the queue

    Declaration
    type IFileUploadStatus = 'validating' | 'queued' | 'uploading' | 'processing' | 'succeeded' | 'failed' | 'canceled' | 'rejected';

    IFileUploadSucceededItem

    type

    a file uploaded, with what the upload resolved to

    Declaration
    type IFileUploadSucceededItem<TResult> = IFileUploadItemBase & {
        status: 'succeeded';
        result: TResult;
    };

    IFileUploadSuccessHandler

    type

    receives a file once it's uploaded

    Declaration
    type IFileUploadSuccessHandler<TResult> = (item: IFileUploadSucceededItem<TResult>) => void;

    IFileUploadSummary

    type

    the queue's counts by status, and its progress weighted by bytes

    Declaration
    type IFileUploadSummary = {
        activeUploads: number;
        canceledFiles: number;
        failedFiles: number;
        progress: IFileUploadProgress;
        queuedFiles: number;
        rejectedFiles: number;
        succeededFiles: number;
        totalFiles: number;
        validatingFiles: number;
    };

    Members

    • activeUploadsproperty
      number
    • canceledFilesproperty
      number
    • failedFilesproperty
      number
    • progressproperty
      IFileUploadProgress
    • queuedFilesproperty
      number
    • rejectedFilesproperty
      number
    • succeededFilesproperty
      number
    • totalFilesproperty
      number
    • validatingFilesproperty
      number

    IFileUploadUploadingItem

    type

    a file sending its bytes

    Declaration
    type IFileUploadUploadingItem = IFileUploadItemBase & {
        status: 'uploading';
    };

    IFileUploadValidatingItem

    type

    a file being checked

    Declaration
    type IFileUploadValidatingItem = IFileUploadItemBase & {
        status: 'validating';
    };

    IFileUploadValidationIssue

    type

    why a file wasn't admitted, with the message to show

    Declaration
    type IFileUploadValidationIssue = IFileUploadErrorDetails & {
        reason: IFileUploadValidationReason;
    };

    IFileUploadValidationReason

    type

    why a file wasn't admitted: a built-in check, or the app's validator

    Declaration
    type IFileUploadValidationReason = 'custom' | 'duplicate-file' | 'empty-file' | 'file-count-exceeded' | 'file-too-large' | 'total-size-exceeded' | 'unsupported-file-type';

    IFileUploadValidator

    type

    checks a file the built-in checks admitted, such as an image's dimensions, resolving to an issue or null

    Declaration
    type IFileUploadValidator = (file: File, context: IFileUploadValidatorContext) => IFileUploadValidationIssue | null | Promise<IFileUploadValidationIssue | null>;

    IFileUploadValidatorContext

    type

    what the app's validator receives besides the file

    Declaration
    type IFileUploadValidatorContext = {
        admittedFiles: readonly File[];
        signal: AbortSignal;
        uploadId: string;
    };

    Members

    • admittedFilesproperty
      readonly File[]

      the files admitted so far, the existing files included

    • signalproperty
      AbortSignal

      aborts when the file is canceled or removed while it's validated

    • uploadIdproperty
      string

    IFormChangesComparable

    type

    a command a form edits, compared by value as JSON

    Declaration
    type IFormChangesComparable = object;

    IThemeColors

    type

    the browser chrome's color in each theme, as a CSS color, such as each theme's background

    Declaration
    type IThemeColors = {
        light: string;
        dark: string;
    };

    Members

    • lightproperty
      string
    • darkproperty
      string

    IThemeInitScriptOptions

    type

    what createThemeInitScript reads the stored preference from

    Declaration
    type IThemeInitScriptOptions = {
        storageKey: string;
        field?: string | undefined;
        defaultPreference?: IThemePreference | undefined;
    };

    Members

    • storageKeyproperty
      string

      the localStorage key holding the preference, such as jg-storage:preferences

    • fieldproperty
      string

      the field of a stored object that holds the preference, such as theme

    • defaultPreferenceproperty
      IThemePreference

      the preference used when none is stored or it isn't valid. Defaults to 'system'

    IUseActiveHeadingOptions

    type

    what useActiveHeading takes

    Declaration
    type IUseActiveHeadingOptions = {
        headingIds: readonly string[];
        topOffsetPx: number;
    };

    Members

    • headingIdsproperty
      readonly string[]

      the ids of the headings to follow, in the order they appear on the page

    • topOffsetPxproperty
      number

      the height of what covers the top of the viewport, such as a sticky header, in pixels; a heading is reached once its top passes this line

    IUseActiveHeadingResult

    type

    what useActiveHeading gives

    Declaration
    type IUseActiveHeadingResult = {
        activeHeadingId: string | null;
        setActiveHeadingId: Dispatch<SetStateAction<string | null>>;
    };

    Members

    • activeHeadingIdproperty
      string | null

      the id of the heading the reader is at, or null before one is known

    • setActiveHeadingIdproperty
      Dispatch<SetStateAction<string | null>>

      sets the active heading, such as when a table of contents link is clicked

    IUseCopyFeedbackOptions

    type

    what useCopyFeedback takes

    Declaration
    type IUseCopyFeedbackOptions = {
        durationSeconds?: number | undefined;
        onError?: ((error: unknown) => void) | undefined;
    };

    Members

    • durationSecondsproperty
      number

      how long isCopied stays true after a copy, in seconds: positive and finite. Defaults to DEFAULT_COPY_FEEDBACK_SECONDS

    • onErrorproperty
      (error: unknown) => void

      receives a failed copy's error, such as to show why it failed. The latest one is called

    IUseCopyFeedbackResult

    type

    what useCopyFeedback returns

    Declaration
    type IUseCopyFeedbackResult = {
        isCopied: boolean;
        copy: (write: () => unknown) => Promise<boolean>;
    };

    Members

    • isCopiedproperty
      boolean

      whether a copy just succeeded, for a moment

    • copyproperty
      (write: () => unknown) => Promise<boolean>

      calls write at once, inside the click that called copy, clearing an earlier copy's state, and resolves to whether it succeeded. It never rejects, and resolves to false at once while another copy runs

    IUseCurrentTimeOptions

    type

    what useCurrentTime takes

    Declaration
    type IUseCurrentTimeOptions = {
        intervalSeconds?: number | undefined;
    };

    Members

    • intervalSecondsproperty
      number

      how often the time moves on, in seconds, positive and finite. Defaults to 60

    IUseDebouncedSearchOptions

    type

    what useDebouncedSearch takes

    Declaration
    type IUseDebouncedSearchOptions<TResult> = {
        query: string;
        enabled?: boolean | undefined;
        search: (query: string, signal: AbortSignal) => Promise<TResult>;
    };

    Members

    • queryproperty
      string

      what the user typed; it's trimmed, and an empty query searches nothing

    • enabledproperty
      boolean

      whether to search at all, such as while a search dialog is open. Defaults to true

    • searchproperty
      (query: string, signal: AbortSignal) => Promise<TResult>

      runs one search, and should stop when the signal aborts, which happens when the query changes, the search is disabled, or the component unmounts

    IUseDebouncedSearchResult

    type

    what useDebouncedSearch gives

    Declaration
    type IUseDebouncedSearchResult<TResult> = {
        results: TResult | undefined;
        isSearching: boolean;
        error: unknown;
        retry: () => void;
    };

    Members

    • resultsproperty
      TResult | undefined

      the last search's results, kept while the next one runs, or undefined before the first, after a failed one, and while the query is empty or the search is disabled

    • isSearchingproperty
      boolean

      whether a search for the current query is waiting or running

    • errorproperty
      unknown

      what the current query's search rejected with, or null

    • retryproperty
      () => void

      searches the current query again, such as from a Retry button after an error

    IUseElementWidthResult

    type

    what useElementWidth returns: the ref to attach, and what select gave for the element's width

    Declaration
    type IUseElementWidthResult<T> = {
        ref: (element: Element | null) => void;
        value: T | undefined;
    };

    Members

    • refproperty
      (element: Element | null) => void

      attaches the element to measure, or detaches it with null

    • valueproperty
      T | undefined

      what select returned for the latest width above 0, or undefined while no element is attached

    IUseFileUploadQueueOptions

    type

    what useFileUploadQueue takes

    Declaration
    type IUseFileUploadQueueOptions<TResult> = IFileUploadPolicyOptions & {
        uploadFile: IFileUploadHandler<TResult>;
        validateFile?: IFileUploadValidator | undefined;
        disabled?: boolean | undefined;
        existingFiles?: readonly File[] | undefined;
        isPreviewEnabled?: boolean | undefined;
        messages?: Partial<IFileUploadMessages> | undefined;
        getErrorFeedback?: ((error: unknown) => IFileUploadErrorFeedback) | undefined;
        controlsRef?: Ref<IFileUploadControls> | undefined;
        onItemsChange?: IFileUploadItemsChangeHandler<TResult> | undefined;
        onFileRejected?: IFileUploadRejectedHandler | undefined;
        onUploadSuccess?: IFileUploadSuccessHandler<TResult> | undefined;
        onUploadFailure?: IFileUploadFailureHandler | undefined;
        onUploadCanceled?: IFileUploadCanceledHandler | undefined;
    };

    IUseFileUploadQueueResult

    type

    what useFileUploadQueue returns: its controls, its items and their summary, the latest announcement, and the policy

    Declaration
    type IUseFileUploadQueueResult<TResult> = IFileUploadControls & {
        announcement: string;
        items: readonly IFileUploadItem<TResult>[];
        policy: IFileUploadPolicy;
        summary: IFileUploadSummary;
    };

    IUseFormChangesOptions

    type

    what useFormChanges compares: the command being edited against the last confirmed one

    Declaration
    type IUseFormChangesOptions<TCommand extends IFormChangesComparable, TNormalizedCommand extends IFormChangesComparable = TCommand> = {
        confirmedCommand: TCommand;
        currentCommand: TCommand;
        normalizeCommand?: ((command: TCommand) => TNormalizedCommand) | undefined;
    };

    Members

    • confirmedCommandproperty
      TCommand

      the command as the server last confirmed it, such as the saved profile

    • currentCommandproperty
      TCommand

      the command the form holds now

    • normalizeCommandproperty
      (command: TCommand) => TNormalizedCommand

      turns a command into the form both are compared in, such as with trimmed text

    IUseFormChangesResult

    type

    whether the form holds changes, and what advances its baseline

    Declaration
    type IUseFormChangesResult<TCommand extends IFormChangesComparable> = {
        confirmChanges: (command: TCommand) => void;
        guardSubmission: (command: TCommand) => boolean;
        hasChanges: boolean;
    };

    Members

    • confirmChangesproperty
      (command: TCommand) => void

      makes a command the baseline, once the server confirmed saving it

    • guardSubmissionproperty
      (command: TCommand) => boolean

      checks, as the form submits, whether a command still differs from the baseline

    • hasChangesproperty
      boolean

      whether the current command differs from the baseline

    IUseIsTextClampedOptions

    type

    what useIsTextClamped takes

    Declaration
    type IUseIsTextClampedOptions = {
        lines: number;
        content?: string | null | undefined;
    };

    Members

    • linesproperty
      number

      the lines the clamp shows, a positive integer, as the element's line-clamp sets them

    • contentproperty
      string | null

      the text, which can change without changing the clamped box's size, so it's measured again

    IUseIsTextClampedResult

    type

    what useIsTextClamped returns: the ref to attach, and whether the text runs past the clamp

    Declaration
    type IUseIsTextClampedResult = {
        ref: (element: HTMLElement | null) => void;
        isClamped: boolean;
    };

    Members

    • refproperty
      (element: HTMLElement | null) => void

      attaches the clamped element, or detaches it with null

    • isClampedproperty
      boolean

      whether the text is taller than the lines the clamp shows; false without an element

    IUseNearViewportOptions

    type

    what useNearViewport takes

    Declaration
    type IUseNearViewportOptions = {
        rootMargin?: string | undefined;
    };

    Members

    • rootMarginproperty
      string

      the margin around the viewport, as IntersectionObserver's rootMargin takes it. Defaults to 0px

    IUseNearViewportResult

    type

    what useNearViewport returns: the ref to attach, and whether the element is near the viewport

    Declaration
    type IUseNearViewportResult = {
        ref: (element: Element | null) => void;
        isNear: boolean;
    };

    Members

    • refproperty
      (element: Element | null) => void

      attaches the element to watch, or detaches it with null

    • isNearproperty
      boolean

      whether the attached element is within the margin of the viewport; false without one

    IUseNewBuildAvailableOptions

    type

    what useNewBuildAvailable takes

    Declaration
    type IUseNewBuildAvailableOptions = {
        entryPath?: string | undefined;
        assetPrefix?: string | undefined;
    };

    Members

    • entryPathproperty
      string

      the page that loads the app, read again to see which build it loads. Defaults to /

    • assetPrefixproperty
      string

      the folder of the build's hashed scripts, whose entry script names the build. Defaults to /assets/

    IUseNewBuildAvailableResult

    type

    what useNewBuildAvailable returns

    Declaration
    type IUseNewBuildAvailableResult = {
        isNewBuildAvailable: boolean;
        check: () => Promise<void>;
    };

    Members

    • isNewBuildAvailableproperty
      boolean

      whether the server serves a newer build than the open page, or one of its chunks is gone

    • checkproperty
      () => Promise<void>

      reads the entry page again now, such as when the app's server connection comes back

    IUseResendCooldownOptions

    type

    what useResendCooldown takes

    Declaration
    type IUseResendCooldownOptions = {
        durationSeconds: number;
        cooldownKey?: string | undefined;
        lastAttemptedAt?: string | number | Date | null | undefined;
        storeId?: string | undefined;
    };

    Members

    • durationSecondsproperty
      number

      how long a resend waits after a send, in seconds: a positive integer

    • cooldownKeyproperty
      string

      what the cooldown is for, such as ${accountId}:verify-email, so it survives a reload of the tab and stays apart from other accounts' and actions'. Without it, nothing is stored

    • lastAttemptedAtproperty
      string | number | Date | null

      when the server says the last send happened, used until the tab sends one itself

    • storeIdproperty
      string

      the jg-storage session store holding the deadlines: letters, digits, underscores, and hyphens. Defaults to 'resend-cooldowns'

    IUseResendCooldownResult

    type

    the cooldown's state, and what starts it

    Declaration
    type IUseResendCooldownResult = {
        isCoolingDown: boolean;
        remainingSeconds: number;
        startCooldown: () => void;
    };

    Members

    • isCoolingDownproperty
      boolean

      whether a resend must wait

    • remainingSecondsproperty
      number

      the whole seconds left, rounded up, 0 when it needn't wait

    • startCooldownproperty
      () => void

      starts the cooldown, once the server acknowledged a send