Overview
Add nds-empty to a container that can have no items, such as search results, a filtered list or a notification list. It works on a <div>, a list, a table and a table body. An item is any child element. The script adds the placeholder while the container has no items, and removes it when one arrives. You write only the class.
Pick another component when:
- the data is still loading: Loading.
- a request failed and the rest of the view still shows: Alert, with
data-status="error", so the user knows that something went wrong. - a request failed and the view has nothing else to show, or the page is a status, such as not found or no permission: Status Section. An empty cart or an empty list stays Empty, even when it is all the page shows: items can arrive without a reload.
- the container has items but none match the user’s filter: Filter shows its own alert. A filtered-out item stays in the container, so the placeholder does not show.
Markup
Variants
The Message and Icon rows go on the element that carries nds-empty, whatever its tag.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Container (default) | — | — | Any block element. The placeholder is a <div>, centered in the container |
| Structure | List | canon #empty-list |
— | A <ul> or an <ol>. The placeholder is an <li>, so the list stays valid |
| Structure | Table | canon #empty-table |
— | nds-empty on the <table>. The script adds a <tbody> when the table has none |
| Structure | Table body | canon #empty-tbody |
— | nds-empty on one <tbody>, for a table with more than one body |
| Action | Action (hint: A button under the message, such as Add a request) | canon #empty-action |
.nds-empty |
A <template data-empty-action> in the container. Each placeholder gets a copy of its content under the message, so the button shows only while the container is empty. The template is never an item, in a <div>, a list or a table |
| Message | Message (hint: Replaces the default text) | [data-empty-message="No requests match your search"] |
.nds-empty |
Replaces the default text. Say why the container is empty, or what the user can do next |
| Icon | Icon (hint: Replaces the default icon) | [data-empty-icon="nds-icon nds-hgi-search-01"] |
.nds-empty |
Replaces the default icon. The value is the whole class of the icon, from any icon set |
Behavior
Live Updates
Add and remove items to watch the placeholder.
Tables
In a table, the placeholder is one row with one cell across every column. The script counts the columns in the header row. Only rows in a <tbody> count as content: the <thead>, <tfoot> and <caption> do not. When the column count changes, call NDS.Empty.refresh() on the element that carries nds-empty, and the cell spans the new count.
Built-in Features
-
Automatic Setup
Every
nds-emptycontainer with no items gets a placeholder, also a container added to the page later. You call nothing. -
Markup That Fits the Container
The placeholder is an
<li>in a list, a row in a table and a<div>elsewhere. In annds-gridcontainer, it spans every column. -
Arabic and English Text
The default message follows the page
lang: «لا يوجد محتوى» in Arabic, and "No content to show" in any other language. -
Live Updates
The placeholder goes away when an item is added, and comes back when the last item is removed. A new
data-empty-messageordata-empty-iconvalue shows at once. -
No Flash at Load
The container stays hidden until the placeholder is in. The page never shows it empty first.
Best Practices
- Show Loading while data loads, not the placeholder. If the request returns no items, the placeholder then shows.
- Write a
data-empty-messagethat says why the container has no items, or what the user can do next. “No requests match your search” tells the user more than “Empty”. - Keep the message to one short sentence. For a button under it, such as “Add a request”, put it in a
<template data-empty-action>in the container. Put longer help outside the container. - Wrap text content in an element. The script counts child elements only, so a container that holds only text still shows the placeholder.
- Do not add your own “no results” markup to the container. The script counts it as an item, so the placeholder never shows.
- When your code counts the items in a container, skip the element with
data-nds-empty-placeholderand the action’s<template>.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
nds-empty-placeholder |
the placeholder <div> or <li> |
Set by the script. Style the placeholder through this class |
nds-empty-message |
the message <span> in the placeholder |
Set by the script. Style the message through this class |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-empty-message |
.nds-empty |
Replaces the default text. A change at run time updates the placeholder |
data-empty-icon |
.nds-empty |
Replaces the default icon, nds-icon nds-hgi-desert. The value becomes the whole class of the icon <i>. A change at run time updates the placeholder |
data-empty-action |
a <template> in .nds-empty |
Its content goes under the message in each placeholder, such as a button. The template is never an item |
data-nds-empty-placeholder |
the placeholder | Set by the script on the placeholder element, and on a <tbody> it adds. Do not set it yourself |
Autocomplete reads the same two attributes for its no-results message.
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_empty.scss.
| Token | Preview | Value |
|---|---|---|
--text-primary-paragraphRead by color | var(--colors-neutral-700)Dark mode var(--colors-neutral-300) | |
--text-secondary-paragraphRead by color | var(--colors-neutral-500)Dark mode var(--colors-neutral-400) |
JavaScript
| Method | Effect |
|---|---|
NDS.Empty.init() |
Fills every empty container and starts to watch the page. The loader calls it once. A second call does nothing |
NDS.Empty.refresh(el) |
Checks one .nds-empty element again: adds or removes its placeholder, and updates the column count of a table placeholder |
Empty fires no events. Call refresh() only for a change the script cannot see, such as a class added to an element already on the page.
The full API is in the banner of _js/nds-empty.js.