Overview
Expandable Content is a box, nds-expandable, around the content, nds-expandable-content. The script measures the content. When it is taller than the max height, the script adds a Show More button.
Pick another component when:
- the content is a set of sections with headings: Accordion
- one line or a few lines of text must fit: Truncate Text
Markup
Variants
Max Height goes on .nds-expandable-content, and Fade on .nds-expandable, each in a style attribute. In a group, write them on every box.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Block (default) | — | — | Long text in a page section: a description, legal text, a specification |
| Structure | Card (demo: + h150) | canon #expandable-card |
— | A card with a long description. nds-expandable goes on the card itself |
| Structure | Group (hint: Show More on one card opens both) (demo: + h150) | canon #expandable-group |
— | Related items that a user compares side by side, such as policy summaries |
| Max Height | 300px (default) | — | .nds-expandable-content |
A text block in a page section |
| Max Height | 150px (id: h150) | --max-height: 150px |
.nds-expandable-content |
A short preview, for cards |
| Fade | 35% (default) | — | .nds-expandable |
The fade is 35% of the max height, and ends above the button |
| Fade | 60% | --mask-fade-percentage: 60% |
.nds-expandable |
A longer fade |
Behavior
Group
Put nds-expand-all on the parent of several expandable boxes. Show More on one box then opens every box in the group, and Show Less closes them all. Only the boxes in the group itself follow: an expandable box inside a box’s content opens and closes by itself.
Built-in Features
-
Automatic Start
The loader starts the script on every
.nds-expandable. You write no script. -
No Layout Shift
CSS limits the content to the max height before the script runs. The page does not jump when the script starts.
-
Height Check
The script measures the content again when its size changes. When the content fits, the script removes the limit and hides the button.
-
Fade
Limited content fades out above the button. No text shows behind the button.
-
Bilingual Labels
The button reads "Show More" and "Show Less" in English, and «عرض المزيد» and «عرض أقل» in Arabic, from the
langof the page. -
Hidden Panels
A box in a hidden panel stays limited until the panel shows. Tabs measures it again when its panel opens.
Best Practices
- Use it for long text that most users do not read in full: descriptions, legal text, specifications, long lists.
- Do not hide main actions or information that the user must read.
- Do not write the button. The script builds it.
- Write
--max-heightin px. The script cannot read other units, such asrem, and shows the button on content that fits. - Set a max height that shows enough text for the user to decide whether to read more. Below 80px, the first paragraph can be hidden.
- Use a group for related items that the user compares, such as a comparison grid or a set of policy summaries.
- After you add expandable boxes to the page, call
NDS.Expandable.init(). - After your script changes
--max-height, callNDS.Expandable.refresh(). A hidden box measures itself again when it is shown.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
nds-expand-btn |
the button the script builds | Places the button at the bottom end of the box. Use it to style the button |
nds-expandable-clip |
an element between .nds-expandable and .nds-expandable-content |
The script adds it, and CSS clips the element while the content is limited. Do not write it |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-state~="expandable" |
.nds-expandable |
The script sets it when the content is taller than the max height, and removes it when the content fits. destroy() removes it. CSS adds the fade and places the button while it is set |
data-state~="expanded" |
.nds-expandable |
The script sets it when the box opens, and removes it when the box closes or the content fits. destroy() removes it |
CSS Custom Properties
| Property | Default | Controls |
|---|---|---|
--max-height |
300px |
The height of the content while it is limited. Set it on .nds-expandable-content or on a parent, in px only. After you change it, call refresh() |
--mask-fade-percentage |
35% |
The length of the fade, as a share of the max height. The fade ends above the button. Set it on .nds-expandable |
JavaScript
The NDS.Expandable script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
The instance is on the box as el.ndsExpandable. It is there once the component starts: code that runs at page load waits for NDS.init.ready.
| Method | Effect |
|---|---|
NDS.Expandable.init() |
Starts every .nds-expandable on the page that has no instance. The loader calls it. Call it after you add boxes to the page |
NDS.Expandable.refresh() |
Measures every box again. Call it after you change --max-height |
NDS.Expandable.create(el) |
Starts one box and returns its instance. Returns the running one when the box already has one, and null when it has no .nds-expandable-content |
NDS.Expandable.destroy(el) |
The same as instance.destroy() |
instance.expand() |
Opens the box. Does nothing on an open box |
instance.collapse() |
Closes the box. Does nothing on a closed box |
instance.toggle() |
Opens a closed box and closes an open one |
instance.refresh() |
Reads --max-height and measures the box again |
instance.getState() |
Returns isExpanded, hasButton (the script built the button, shown or hidden), maxHeight (px) and actualHeight (the full content height in px) |
instance.destroy() |
Removes the button, the states and the size watch |
| Event | Fired on | Detail |
|---|---|---|
nds:expandable:expanded |
.nds-expandable, after it opens |
container, content, button, isExpanded |
nds:expandable:collapsed |
.nds-expandable, after it closes |
container, content, button, isExpanded |
Both events bubble. In a group, each box fires its own event.
The full API is in the banner of _js/nds-expandable.js.