Lightweight lightbox library for Svelte
Lightweight Lightbox, but very customizable Svelte component library. There is also support for mobile devices. Note that this first version of this library, if you notice any bug, please report it to this library's GitHub repository to the 'Issues' section.
npm i svelte-lightbox -D
- Make Svelte page
-
Include
import { Lightbox } from 'svelte-lightbox';
into that file. -
Make
<Lightbox>
component and image you need to display put inside slot (as its child).
-
Include
import { LightboxGallery, GalleryImage, GalleryThumbnail } from 'svelte-lightbox';
into that file. -
Make
<LightboxGallery>
component and append to it list of<GalleryImage>
, which each<GalleryImage>
contains desired image (or it could be video). -
Prepend
<LightboxGallery>
with element or svelte:fragment with prop slot equal to"thumbnail"
, under this element place your thumbnail layout, which is basically layout with images wrapped inside<GalleryThumbnail>
.
<script>
import {
Lightbox,
LightboxGallery,
GalleryThumbnail,
GalleryImage
} from 'svelte-lightbox'
let lightboxProgrammaticController
</script>
<!-- Lightbox with image same as thumbnail -->
<Lightbox description="Simple lightbox">
<img src="path" alt="Simple lightbox">
</Lightbox>
<!-- Lightbox with different image from thumbnail -->
<Lightbox description="Lightbox with thumbnail and image">
<img slot="thumbnail" src="path/thumbnail.png" alt="Thumbnail">
<img src="path/image.png" alt="Lightbox image">
</Lightbox>
<!-- Programmatically controlled lightbox without thumbnail -->
<Lightbox enableFallbackThumbnail={false} bind:programmaticController={lightboxProgrammaticController}
description="Simple lightbox">
<img src="path" alt="Simple lightbox">
</Lightbox>
<LightboxGallery>
<!-- Layout-->
<svelte:fragment slot="thumbnail">
<div class="sample-class-1">
<GalleryThumbnail>
<img src="./thumbnail-0.jpg" alt="Simple lightbox">
</GalleryThumbnail>
<div class="sample-class-2">
<div class="sample-class-3">
<img src="./thumbnail-0.jpg" alt="Simple lightbox">
</div>
<div class="sample-class-4">
<img src="./thumbnail-0.jpg" alt="Simple lightbox">
</div>
</div>
</div>
</svelte:fragment>
<GalleryImage>
<img src="./image-0.jpg" alt="Simple lightbox">
</GalleryImage>
<GalleryImage>
<img src="./image-1.jpg" alt="Simple lightbox">
</GalleryImage>
<GalleryImage>
<img src="./image-2.jpg" alt="Simple lightbox">
</GalleryImage>
</LightboxGallery>
If you need more usage examples, you should see demo page code at this library GitHub repository.
This library mainly consists out of 2 components which are <Lightbox>
and <LightboxGallery>
, but their customization
is significant part of using them.
This component is meant to be used to display image, which is clickable, that click will trigger action to open same or different image in larger scale.
Image's title that is displayed below the image (in a lightbox), feel free to use HTML (eg, <br>
, <span>
). Everything
in a lightbox footer is aligned to the left, but you can override it by putting it into <span>
which would allow you to
apply a different style or a class of your choosing.
Image's description that is, similarly to the title, displayed under the image (in a lightbox). The difference between
those two are the title is wrapped in <h2>
and description is wrapped in <h5>
.
Select between these 3 presets:
-
''
- Default preset, doesn't make any drastic behavior changes, just displays image in its maximum size and fits into viewport. -
'fullscreen'
- Makes image fullscreen to cover at least one of axes. -
'scroll'
- Enables scrolling big image instead of making it smaller to fit into the screen.
Customization object contains these props, which represent html props of their key (e.g. closeButtonProps = html props of close button of).
- closeButtonProps:
HTMLButtonElement
- lightboxFooterProps:
HTMLDivElement
- lightboxHeaderProps:
HTMLDivElement
- coverProps:
HTMLDivElement
- lightboxProps:
HTMLDivElement
- thumbnailProps:
HTMLDivElement
If you'd like to change global styles, feel free to override these global CSS classes instead: .svelte-lightbox-main
,
.svelte-lightbox-body
, .svelte-lightbox-overlay
, .svelte-lightbox-header
, .svelte-lightbox-footer
and .svelte-lightbox-thumbnail
.
Duration of lightbox toggle, in milliseconds. Based on this number are calculated transitions for lightbox cover. Default
300
.
Keeps body scroll while lightbox is open. Default false
.
Enables image inside lightbox to resize above its resolution. Default false
.
Enables inferring thumbnail from lightbox content, if thumbnail isn't specified. Default true
.
Enables closing lightbox on keydown which is equal to escape button. Default true
.
Anywhere user clicks when modal is opened, closes it. Default false
.
Shows close button. Default true
.
Allows you to programmatically control lightbox visibility without programmaticController. Default varies on user activity.
Object with these basic control functions:
- toggle
() => void
- toggles lightbox (opened -> closed, vice versa) - open
() => void
- opens lightbox - close
() => void
- closes lightbox
Lightbox component with explicitly defined thumbnail layout and multiple images, allows listing thorough all images within gallery. According to almost 2 years experience of using this component (in early versions), I found out, that gallery without thumbnail layout doesn't give any sense in most of the cases, so I decided to embed this feature in design of this component.
Same as in <Lightbox>
title, but this is same in whole gallery. <GalleryImage>
can override this title for specific image.
Same as in <Lightbox>
description, but this is same in whole gallery. <GalleryImage>
can override this description for
specific image.
Same as in <Lightbox>
.
Same as in <Lightbox>
.
Same as in <Lightbox>
.
Same as in <Lightbox>
.
Same as in <Lightbox>
.
Same as in <Lightbox>
.
Same as in <Lightbox>
.
Same as in <Lightbox>
.
Same as in <Lightbox>
.
Number which sets visibility of image with id equal to it. Also optional, this is used for programmatic selecting of visible image when Lightbox is opened.
Allows customizing gallery arrows.
Sets arrow color, valid value is any valid css color.
Sets arrow charter in edge cases (0th and last item of gallery). Valid values:
-
''
- Leaves arrow inactive and disables particular arrow on keyboard in edge case. -
'hide'
- Hides arrow and disables particular arrow on keyboard in edge case. -
'loop'
- Arrows are always active and keyboard arrows as well. For instance when user clicks left arrow on first image, last image will be displayed.
Enables navigation in gallery using keyboard arrows. Default true
.
Object with these basic control functions:
- toggle
() => void
- toggles lightbox (opened -> closed, vice versa) - open
() => void
- opens lightbox - openImage
(imageId: number) => void
- opens lightbox at specific image - close
() => void
- closes lightbox
Link to gallery image, it can be set to any number within gallery images, starting with 0. Default is order of
<GalleryThumbnail>
, so let's say you have 3 thumbnails under <LightboxGallery>
, the first thumbnails has id 0, next one
1 and third 2.
Same as <Lightbox>
title, but this is image specific, that means, it will be only shown for particular image.
Same as <Lightbox>
description, but this is image specific, that means, it will be only shown for particular image.
This library by design allows you to construct custom lightbox by providing you with basic building block of lightbox.
If you want to use this advanced functionality, you are supposed to read code (I recommend reading it from GitHub, code
from NPM is already processed) of basic building blocks and examples of their usage (<Lightbox>
, <LightboxGallery>
)
as well, this documentation is only brief overview. You can take available building blocks and lay them down in different
way, or replace some of them by your own tweaked variants. Basic building blocks are:
Name says it all.
Makes content of slot direct child of <body>
, this can be especially useful when using CSS frameworks, which can break
z-index
effect in some cases (element with higher index is lower than element with lower index).
Cover behind lightbox modal, dimmed background.
Name says it all.
Header of lightbox modal, contains close button as well as window keypress binding.
Body of lightbox modal, contains lightbox image.
Footer of lightbox modal, contains title, description of image and gallery counter.
Layout with image space, left and right arrow, internal logic of gallery.
Buttons which allow you to move within gallery.
There is 1 thing that has to be localized and that is gallery counter, that what shows user where in gallery is located
(e.g. Image 2 of 4). Gallery counter text can be customized by overriding its generator. That can be done by importing
i18n from 'svelte-lightbox' and then setting generateLocalizedGalleryCounter
to desired generator.
(activeImage: number, imageCount: number): string => {
return `Image ${activeImage + 1} of ${imageCount}`
}
Every issue or code contribution is welcome.
It's free, this component is under MIT license.