Share
A share button through the Web Share API, with a copy-link fallback.
share adds a share button. It is off by default.
Live demo. Click a thumbnail to open it.
<Lightbox share {...props} />
<Lightbox
share={{ url: (slide, index) => `https://example.com/photos/${index + 1}` }}
{...props}
/>How it works
- Where the system share sheet exists, the button calls
navigator.sharewith the link and the slide's stringtitle, or itsalt. - Where it does not, the link is copied with
navigator.clipboard. The icon turns intoicons.linkCopiedfor a moment andlabels.linkCopiedis announced. - Cancelling the share sheet does nothing.
- If the link can be neither shared nor copied,
labels.shareErroris announced. - When neither API exists, for example over plain
http, the button is not rendered.
No third-party script, SDK or share URL is involved, so nothing changes in your Content Security Policy.
Link to the open slide
The link is window.location.href by default. With useLightboxHash, that URL already points at the open slide:
const [index, setIndex] = useLightboxHash("photo", { count: slides.length });
<Lightbox
share
slides={slides}
open={index >= 0}
index={Math.max(index, 0)}
onIndexChange={setIndex}
onClose={() => setIndex(-1)}
/>;