Overview
Share is a .nds-share wrapper of share buttons, in a Dropmenu or in a row. Five buttons are built in: X, LinkedIn, WhatsApp, Copy Link, and Device Share, which opens the device’s own share sheet for any app on a phone. The script finds each one by its class. data-share-href adds any other target, such as Facebook or email. By default, every button shares the current page.
Pick another component when:
Markup
<div class="nds-share nds-dropmenu">
<button class="nds-btn nds-secondary-outline nds-dropmenu-trigger" aria-label="Share Page" data-i18n-aria-label="share.share_page">
<i class="nds-icon nds-hgi-share-01" aria-hidden="true"></i>
<span class="nds-label" data-i18n="share.share_page">Share Page</span>
</button>
<div class="nds-dropmenu-menu" hidden>
<button class="nds-btn nds-subtle nds-dropmenu-item nds-share-x" type="button" aria-label="Share on X" data-i18n-aria-label="share.share_on_x">
<i class="nds-icon nds-hgi-new-twitter" aria-hidden="true"></i>
<span class="nds-label">X</span>
</button>
<button class="nds-btn nds-subtle nds-dropmenu-item nds-share-linkedin" type="button" aria-label="Share on LinkedIn" data-i18n-aria-label="share.share_on_linkedin">
<i class="nds-icon nds-hgi-linkedin-02" aria-hidden="true"></i>
<span class="nds-label">LinkedIn</span>
</button>
<button class="nds-btn nds-subtle nds-dropmenu-item nds-share-whatsapp" type="button" aria-label="Share on WhatsApp" data-i18n-aria-label="share.share_on_whatsapp">
<i class="nds-icon nds-hgi-whatsapp" aria-hidden="true"></i>
<span class="nds-label">WhatsApp</span>
</button>
<button class="nds-btn nds-subtle nds-dropmenu-item nds-share-copy" type="button" aria-label="Copy Link" data-i18n-aria-label="share.copy_link" data-copy-label data-no-auto-close>
<i class="nds-icon nds-hgi-link-04" aria-hidden="true"></i>
<span class="nds-label" data-i18n="share.copy_link">Copy Link</span>
</button>
</div>
</div>
Variants
Custom Link is two rows of one choice: write both attributes on .nds-share. Icon Only Trigger is four rows of one choice on the menu’s trigger button, so it is off on the Inline structures. Each Custom Target is three rows of one choice, one per structure: a menu item at the end of the menu, or a button after Copy Link in the Inline row or in Inline Icons. Device Share is three rows of one choice in the same places. Write its button hidden: the script shows it where the browser can open the device’s share sheet.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Menu (default) | — | — | A button that opens a menu of the share buttons. Use it where space is short, such as a hero or a toolbar |
| Structure | Inline | canon #share-inline |
— | The share buttons in a row, visible at all times. Use it at the end of an article |
| Structure | Inline Icons | canon #share-inline-icons |
— | The row with icons only, for a card or a narrow space. Each button keeps its aria-label, and its title shows as a tooltip on hover |
| Size | LG (default) | — | — | 40px buttons. It needs no class |
| Size | MD | .nds-md |
.nds-share > .nds-btn |
32px buttons, for a card or a toolbar. Write it on the trigger, or on every button in the row |
| Size | SM | .nds-sm |
.nds-share > .nds-btn |
24px buttons, for a dense list or a table row. Write it on the trigger, or on every button in the row |
| Custom Link | Custom Link (hint: Share another page, not the current one) | [data-share-url="https://www.example.gov.sa/news/eid-service-hours"] |
.nds-share |
Shares the link and title of another page, such as a news card or a search result |
| Custom Link | Custom Link (hint: Share another page, not the current one) | [data-share-title="Service Hours During Eid"] |
.nds-share |
The title that X and WhatsApp send with the link |
| Icon Only Trigger | Icon Only Trigger (hint: Hide the trigger’s label) | .nds-icon-only |
.nds-dropmenu-trigger |
The trigger shows the share icon only, with a tooltip on hover. Its aria-label names what is shared |
| Icon Only Trigger | Icon Only Trigger (hint: Hide the trigger’s label) | .nds-tooltip |
.nds-dropmenu-trigger |
The tooltip that names the hidden label |
| Icon Only Trigger | Icon Only Trigger (hint: Hide the trigger’s label) | [data-tooltip-hover="500"] |
.nds-dropmenu-trigger |
Opens the tooltip on hover, so a click still opens the menu |
| Icon Only Trigger | Icon Only Trigger (hint: Hide the trigger’s label) | [title="Share Page"] |
.nds-dropmenu-trigger |
The tooltip text: the same words as the aria-label |
| Device Share | Device Share (hint: Opens the phone’s share sheet, for any app) | canon #share-device-item |
.nds-dropmenu-menu |
The device’s share sheet, as a menu item. It reaches every app on the device, such as Telegram, Snapchat, SMS and AirDrop |
| Device Share | Device Share (hint: Opens the phone’s share sheet, for any app) | canon #share-device-button |
.nds-share-copy:not(.nds-dropmenu-item):not(.nds-icon-only) (after) |
The same button in the Inline row |
| Device Share | Device Share (hint: Opens the phone’s share sheet, for any app) | canon #share-device-icon |
.nds-share-copy.nds-icon-only (after) |
The same button, icon only, in Inline Icons |
| Custom Target (any) | Facebook (hint: Adds a button that uses data-share-href) | canon #share-facebook-item |
.nds-dropmenu-menu |
A target added with data-share-href, as a menu item |
| Custom Target (any) | Facebook (hint: Adds a button that uses data-share-href) | canon #share-facebook-button |
.nds-share-copy:not(.nds-dropmenu-item):not(.nds-icon-only) (after) |
The same target as a button in the Inline row |
| Custom Target (any) | Facebook (hint: Adds a button that uses data-share-href) | canon #share-facebook-icon |
.nds-share-copy.nds-icon-only (after) |
The same target as an icon-only button in Inline Icons |
| Custom Target (any) | Telegram (hint: Adds a button that uses data-share-href) | canon #share-telegram-item |
.nds-dropmenu-menu |
A target added with data-share-href, as a menu item |
| Custom Target (any) | Telegram (hint: Adds a button that uses data-share-href) | canon #share-telegram-button |
.nds-share-copy:not(.nds-dropmenu-item):not(.nds-icon-only) (after) |
The same target as a button in the Inline row |
| Custom Target (any) | Telegram (hint: Adds a button that uses data-share-href) | canon #share-telegram-icon |
.nds-share-copy.nds-icon-only (after) |
The same target as an icon-only button in Inline Icons |
| Custom Target (any) | Email (hint: Open the mail app with the link) | canon #share-email-item |
.nds-dropmenu-menu |
A target added with data-share-href, as a menu item |
| Custom Target (any) | Email (hint: Open the mail app with the link) | canon #share-email-button |
.nds-share-copy:not(.nds-dropmenu-item):not(.nds-icon-only) (after) |
The same target as a button in the Inline row |
| Custom Target (any) | Email (hint: Open the mail app with the link) | canon #share-email-icon |
.nds-share-copy.nds-icon-only (after) |
The same target as an icon-only button in Inline Icons |
Behavior
Copy Link in a Menu
data-no-auto-close on the Copy Link item keeps the menu open after the click. The item shows the checkmark for 2 seconds, then the script closes the menu. In the Inline structures, nothing closes, so the attribute is not needed.
Custom Link
data-share-url and data-share-title on .nds-share replace the page link and the page title. A relative link, such as /news/12, becomes a full link on the current site. A link whose scheme is not http, https, mailto or tel is ignored, and the buttons share the current page.
Device Share
.nds-share-device opens the device’s own share sheet with the link and the title. The user picks any app on the device, not only the built-in targets. The button ships with hidden, and the script shows it only where the browser has navigator.share: most phones, and Chrome and Edge on Windows. Elsewhere it stays hidden, and Copy Link still covers every app. Closing the sheet does nothing.
Custom Target
data-share-href on a button in .nds-share adds a target that the built-in buttons do not cover. It holds the target’s share link. The script replaces {url} and {title} with the encoded link and title, then opens the link in a 600×400 window. A mailto: or tel: link opens the mail or phone app instead. The Custom Target options in the builder show the links for Facebook, Telegram and email.
Built-in Features
-
Automatic Start
One click listener on the page serves every share button, including the buttons you add later. You write no script.
-
Social Targets
X, LinkedIn and WhatsApp open their share page in a 600×400 window. X and WhatsApp send the title with the link. LinkedIn sends the link only.
-
Copy Link Feedback
Copy Link uses the Copy script: a checkmark for 2 seconds, the label swap and a screen reader announcement.
-
Multiple Instances
A page can hold any number of
.nds-sharewrappers. Each reads its owndata-share-urlanddata-share-title. -
Named Menu
The script adds
.nds-share-menuto each share menu, a class to style that menu by.
Best Practices
- Use the default, with no attributes, to share the current page, such as in the hero. It reads the link and title of each page, so no page needs its own setting.
- Use Custom Link to share one item on a page: a news card, a search result, a file.
- In a list, put one share in each row. Do not put a page share next to a row share.
- Give the trigger an
aria-labelthat names what is shared: “Share Page”, “Share Article”, “Share Report”. - Give every icon-only button a Tooltip:
nds-tooltip,data-tooltip-hover="500"and atitlewith the same words as thearia-label. The label is hidden, so the tooltip shows a sighted user what the icon does. - Keep
data-no-auto-closeon the Copy Link item in a menu. Without it, the menu closes before the user sees the checkmark. - Keep
data-copy-labelon the Copy Link item, with no value. The label confirms the copy to a sighted user in the page’s language, and a screen reader says the same word. - Keep the
nds-share-*class on each built-in button. The script finds each one by its class. Leave out a button you do not need. - Give a custom target an icon, a label and an
aria-label, like the built-in buttons. - The Inline buttons take any Button style, such as
nds-subtle, and size, such asnds-sm. Use one style and one size for all of them.
API
Classes
| Class | Element | Effect |
|---|---|---|
.nds-share |
the wrapper | Holds the buttons in a row with a small gap. Add .nds-dropmenu for the Menu structure |
.nds-share-x |
a button in .nds-share |
Opens the X share page with the title and the link |
.nds-share-linkedin |
a button in .nds-share |
Opens the LinkedIn share page with the link |
.nds-share-whatsapp |
a button in .nds-share |
Opens WhatsApp with the title and the link in one message |
.nds-share-copy |
a button in .nds-share |
Copies the link and shows the checkmark |
.nds-share-device |
a button in .nds-share |
Opens the device’s share sheet with the title and the link. Write it hidden: init() shows it where the browser has navigator.share |
.nds-share-menu |
the .nds-dropmenu-menu in .nds-share |
The script adds it at init(). Labels in the menu stay on one line |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-share-url |
.nds-share |
The link to share. Default: the current page. A relative link becomes a full link. A scheme other than http, https, mailto or tel falls back to the current page |
data-share-title |
.nds-share |
The title that X and WhatsApp send, and that fills {title}. Default: document.title |
data-share-href |
a button in .nds-share |
The share link of a custom target. The script replaces {url} and {title} with the encoded values. A scheme other than http, https, mailto or tel does nothing. An nds-share-* class on the same button wins |
data-copy-label |
.nds-share-copy |
The label text while the checkmark shows. With no value: “Copied” in the page’s language. Read by Copy |
data-copy-announce |
.nds-share-copy |
The text the live region announces after the copy. Default: data-copy-label, then “Copied” («تم النسخ» on an Arabic page). Read by Copy |
data-no-auto-close |
.nds-share-copy in a menu |
Read by Dropmenu: a click on the item leaves the menu open. Share closes the menu when the checkmark ends |
data-status="success" |
.nds-share-copy |
Copy sets it after a copy, and removes it after 2 seconds. While it is set, the button shows the success color |
aria-disabled="true" |
.nds-share-copy |
Copy sets and removes it with data-status |
JavaScript
The NDS.Share 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.
| Method | Effect |
|---|---|
NDS.Share.init() |
Adds .nds-share-menu to every share menu and listens for clicks on the share buttons. The loader calls it. A second call replaces the listener |
Share fires no events. After you add a share menu to the page, call init() again, so the new menu gets .nds-share-menu. New buttons need no call.
var card = document.querySelector('#news-card-12');
card.insertAdjacentHTML('beforeend', shareMenuHtml);
NDS.Share.init();
The full API is in the banner of _js/nds-share.js.