Skip to content

Expandable text

Pro

Text cut to a few lines with "See more", shown only when it is cut, that expands it in place: posts, comments, reviews and bios. Not an M3 component (@windmorph/react-pro).

import { ExpandableText } from '@windmorph/react-pro';

Part of Windmorph Pro, paid components on the free core, not on sale yet. See the Pro guide for what it includes and to register interest.

Props

ExpandableText 12 props

  • childrentype ReactNodeRequired

    The text, or any inline content (links, mentions). Line breaks in it are kept.

  • linestype numberdefault 5

    The number of lines shown while collapsed.

  • collapsibletype booleandefault false

    Show "See less" while expanded, to collapse it again.

  • expandedtype boolean

    Whether the text is expanded (controlled).

  • defaultExpandedtype booleandefault false

    Whether the text starts expanded (uncontrolled).

  • astype "div" | "p"default "div"

    The element to render: div, or p for a paragraph of plain text.

  • reftype Ref<HTMLElement>

    The root element (as).

Events

  • onExpandedChangetype ((expanded: boolean) => void)

    Called when the text expands or collapses.

Text

  • labelstype Partial<ExpandableTextLabels>

    The buttons' words, so they come from the app's translations. A LabelsProvider sets them for the whole app.

Also accepts the root element's HTML attributes (id, data-*, event handlers).

Spec status

Not part of Material Design 3: a Windmorph Pro component, built on M3's colour roles, type scale, shapes and motion. Its variants and configurations each link to a live example.

  • Cut to a number of lines (lines), with "See more"

    Built

    Configuration · Post

  • Collapsible again, with "See less" (collapsible)

    Built

    Configuration · Collapsible

  • Controlled (expanded)

    Built

    Configuration · Controlled

  • Translated buttons (labels)

    Built

    Configuration · Collapsible

Theming & tokens

The text and the buttons take the surrounding type and colour: set them with className (a type role and a colour role, such as text-body-large text-on-surface). The buttons use the body roles' emphasized weight and underline on hover, focus and press. Set the tokens below on the text or any ancestor (its root carries data-tokens="wm-expandable-text"), and style the parts with classNames (root, content, button).

Expandable text tokens
VariableDefaultPaints
--wm-expandable-text-button-weightvar(--md-sys-typescale-body-large-emphasized-weight)“See more” and “See less”: the body roles’ emphasized weight.
--wm-expandable-text-fade-length2emHow far the cut line fades out before “See more”.
--wm-expandable-text-less-gap4pxBetween the text and “See less”.