Modal
Examples
Disabled footer buttons
Nested modals — reference-counted body scroll lock
A menu opened inside a modal
Two dismissible surfaces at once. Escape closes the menu on top and leaves the modal open; a second Escape then closes the modal. Ownership is the order they opened in, not where they sit in the DOM.
A select opened inside a modal
Two dismissible surfaces at once. Escape closes the select on top and leaves the modal open; a second Escape then closes the modal. An outside click follows the same ordering. Ownership is the order they opened in, not where they sit in the DOM.
Changing debounceTime while a modal is open
debounceTime is read each time a dismissal arrives, so a new value applies to the modal
that is already open without resetting when it last fired. Overlay clicks and Escape are counted here
instead of closing the modal; use the Close button to leave.
Documentation
A dialog overlay component that renders on top of the page with configurable size, alignment, header (with left/right images and text), footer (with primary/secondary Button components), and transition animations. It locks body scroll on mount and optionally handles hardware back-press navigation. The overlay supports click-to-dismiss (debounced) and Escape key handling. The content panel takes no role/aria-modal by default (unchanged from prior behavior); setting role opts into role="dialog" (or role="alertdialog") plus aria-modal="true", with an optional ariaLabel for its accessible name. The primary example on the Modal demo page opts in with role="alertdialog" and an ariaLabel equal to its "Confirm Action" heading, since a confirmation that asks for an answer is an alert dialog rather than an ordinary one. Tab is trapped inside the panel, focus moves in on mount, and returns to whatever was focused before the modal opened once it closes.
Usage
<script>
import { Modal } from '@juspay/svelte-ui-components';
</script>
<Modal />
Props
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
| restoreFocusTo | HTMLElement / getter / null |
No | omitted | Optional focus destination snapshotted per mounted open; resolved once at actual teardown after cleanup. Null, invalid/disconnected/foreign-document targets or getter errors skip restoration without fallback. Property-only on custom elements. |
| size | ModalSize = 'large' | 'medium' | 'small' | 'fit-content' |
No | 'fit-content' |
Controls the height of the modal content panel. 'small'=20vh, 'medium'=50vh, 'large'=80vh, 'fit-content'=auto with 80vh max. |
| align | ModalAlign = 'top' | 'center' | 'bottom' |
No | 'center' |
Vertical alignment of the modal within the viewport. 'top' aligns to the top (flex-start), 'center' centers vertically, 'bottom' aligns to the bottom (flex-end). |
| showOverlay | boolean |
No | true |
When true, shows a dark semi-transparent overlay behind the modal. When false, the overlay is transparent with pointer-events disabled. |
| supportHardwareBackPress | boolean |
No | false |
When true, pushes a history state on mount so that pressing the device back button triggers onclose instead of navigating away. Cleans up on destroy. |
| enableTransition | boolean |
No | true |
When true, the modal content animates in/out using fly or fade transitions via ModalAnimation. |
| transitionType | ModalTransition = 'IN' | 'ALL' |
No | 'ALL' |
Controls transition behavior. 'ALL' animates both in and out transitions. 'IN' only animates the in-transition (content disappears instantly on close). |
| entryAnimation | ModalEntryAnimation = 'fade' | 'slide-up' | 'slide-down' |
No | - |
Overrides the default per-align entry transition. 'slide-up'/'slide-down' reuse the same fly distance/duration as bottom/top alignment — e.g. pair with align="center" for a bottom-sheet-style slide-up dialog. Unset keeps the existing per-align default (fly for top/bottom, fade for center). |
| header | { leftImage?: string; rightImage?: string; text?: string; testId?: string; buttonTestId?: string; buttonAriaLabel?: string; } |
No | {} |
Object configuring the modal header bar. leftImage: URL for left icon (e.g., back arrow); rightImage: URL for right icon (e.g., close button); text: header title text; testId/buttonTestId: test selectors; buttonAriaLabel: accessible name (rendered as aria-label) for the right image's role="button" wrapper — required for screen readers since the wrapper carries no visible text. |
| footer | { primaryButton?: ButtonProperties; secondaryButton?: ButtonProperties; } |
No | - |
Object configuring footer action buttons. primaryButton: ButtonProperties for the main action button; secondaryButton: ButtonProperties for the alternate action button. |
| debounceTime | number |
No | 700 |
Milliseconds during which a repeat overlay click, Escape or hardware back-press is dropped after one has fired; the three share one window. Read when each dismissal arrives, so changing it while the modal is open applies to the next one without forgetting when the last fired. |
| leftImageTestId | string |
No | - |
Value for data-pw on the left header image wrapper. |
| leftImageAriaLabel | string |
No | - |
Accessible name (rendered as aria-label) for the left header image's role="button" wrapper (e.g., a back control) — required for screen readers since the wrapper carries no visible text. |
| overlayAriaLabel | string |
No | - |
Accessible name for the independent native backdrop button. Defaults to "Dismiss modal" when a dismissal handler exists. Generic panels retain Enter/Space dismissal; named dialogs keep the backdrop outside their Tab cycle. |
| testId | string |
No | - |
Value for data-pw on the modal overlay container. |
| classes | string |
No | - |
CSS class string applied to the component's top-level element. Useful for theming — define classes with CSS variable overrides and pass them to create variant styles. |
| overlayBackdropFilter | string |
No | - |
CSS backdrop-filter value applied to the overlay (e.g. "blur(6px)"). Sets --modal-overlay-backdrop-filter inline on the overlay div. Default: none (no blur). |
| overlayFadeIn | boolean |
No | false |
When true, the overlay backdrop fades in on mount instead of appearing instantly. Pairs well with entryAnimation="slide-up" for a softer combined entrance. |
| usePortal | boolean |
No | false |
When true, mounts the modal overlay at the root of the tree it lives in — document.body normally, or the shadow root inside <sui-modal> — so it escapes any ancestor clipping or stacking contexts. Useful inside overflow: hidden or transform containers. The root is chosen rather than always document.body because Svelte scopes a custom element's CSS to its shadow root: an overlay moved into the light DOM keeps its markup and loses every rule scoped to that root, so through <sui-modal> it would render unstyled. |
| ariaLabel | string |
No | - |
Accessible name for the modal, rendered as aria-label on the content panel (the element carrying role/aria-modal when role is set). Without it a screen reader announces an unnamed dialog. |
| ariaDescribedby | string |
No | - |
id of the element that describes the modal, rendered as aria-describedby on the content panel. Point an alertdialog at its message. Unset by default. |
| role | ModalRole = 'dialog' | 'alertdialog' |
No | - (unset) |
ARIA role for the content panel. Unset by default — the panel renders no role and no aria-modal, matching behavior before this prop existed, so an existing consumer who never touches role sees no DOM/ARIA change. Set explicitly to opt in: the panel then also carries aria-modal="true". Use 'alertdialog' for a dialog that interrupts to demand an immediate response (e.g. a destructive confirmation). |
<sui-modal> adds two props that do not exist on the Svelte component: modalRole and
modalAriaLabel (attributes modal-role / modal-aria-label). They are forwarding aliases
for the role and ariaLabel rows above, and exist only because role and aria-label are
reserved accessors on every custom element. See the Web Component section below.
Snippets
Svelte 5 Snippet props — pass content blocks to the component.
| Snippet | Type | Description |
|---|---|---|
| content | Snippet |
A Svelte 5 Snippet for the main modal body content. The modal only renders when this snippet is provided. |
| footerSnippet | Snippet |
A Svelte 5 Snippet that replaces the default footer buttons with custom content. Takes precedence over the footer prop. |
Events
| Event | Type | Description |
|---|---|---|
| onclose | () => void |
Fires when the hardware back button is pressed (only when supportHardwareBackPress is true). Does NOT fire on overlay click or Escape key — those trigger onoverlayclick instead. |
| onheaderrightimageclick | (event: MouseEvent) => void |
Fires when the right image in the header is clicked (e.g., close button). |
| onheaderleftimageclick | (event: MouseEvent) => void |
Fires when the left image in the header is clicked (e.g., back arrow). |
| onprimarybuttonclick | (event: MouseEvent) => void |
Fires when the primary footer button is clicked. |
| onsecondarybuttonclick | (event: MouseEvent) => void |
Fires when the secondary footer button is clicked. |
| onoverlayclick | () => void |
Fires when the dark overlay background is clicked (outside the modal content). Also fires when the Escape key is pressed. Debounced by debounceTime milliseconds. |
| onkeydown | (event: KeyboardEvent) => void |
Fires when any key is pressed while the modal is open (attached to the window). |
| ondismiss | () => void |
Fires on every path that dismisses the modal from outside it: an overlay click, Escape, and a hardware back-press. A superset of onoverlayclick (backdrop/Escape only) and onclose (hardware back-press/autoDismissAfter only) — wire this one callback instead of both when all you need is "the modal wants to close". Both existing events still fire unchanged alongside it. |
CSS Variables
Override these custom properties to theme the component.
| Variable | Default | CSS Property | Description |
|---|---|---|---|
--modal-width |
100vw |
width | Width of the modal overlay container. |
--modal-height |
100vh |
height | Height of the modal overlay container. |
--modal-z-index |
15 |
z-index | Z-index stacking order of the modal overlay. |
--modal-margin |
- |
margin | Outer margin of the modal overlay. |
--modal-overlay-background-color |
#00000066 |
background-color | Overlay backdrop color. Prefer this modal-specific token over the generic --background-color. |
--background-color |
#00000066 |
background-color | Legacy alias for --modal-overlay-background-color (kept for backward compatibility). |
--modal-overlay-backdrop-filter |
none |
backdrop-filter | Backdrop filter applied to the overlay (e.g. blur(6px)). Can also be set via the overlayBackdropFilter prop. |
--modal-overlay-open-transition-duration |
350ms |
opacity | Duration of the overlay's fade-in (only rendered when overlayFadeIn is true). Falls back through --motion-duration. Full detail in OverlayAnimation.md. |
--modal-overlay-close-transition-duration |
350ms |
opacity | Duration of the overlay's fade-out on unmount. Falls back through --motion-duration. Full detail in OverlayAnimation.md. |
--modal-content-background-color |
#ffffff |
background-color | Background color of the modal content panel. |
--modal-border-radius |
0px |
border-radius | Corner rounding of the modal content panel. |
--modal-content-overflow |
auto |
overflow | Overflow behavior of the modal content panel. |
--modal-content-border-top |
- |
border-top | Top border of the modal content panel. |
--modal-max-height |
calc(100dvh - var(--modal-viewport-gutter, 32px)) |
max-height | Viewport cap applied to the content panel at every size. Keeps tall modals (e.g. a size height var overridden to fit-content) from growing past the screen — the body scrolls internally and the footer stays visible. |
--modal-viewport-gutter |
32px |
max-height, max-width | Total space reserved around a centered dialog. Read by the default height cap and by the default centered width ceiling (--modal-center-max-width). For size small, medium and large one token therefore governs both axes; fit-content height is bounded by --modal-fit-content-max-height instead. Needs a length with a unit: a unitless 0 makes the calc() invalid and silently drops both caps, the height cap included, so write 0px. Set it on the overlay (classes) or any ancestor. |
--modal-slot-content-min-height |
0 |
min-height | Min-height of the scrollable content area. The 0 default lets it shrink inside the capped panel so --modal-overflow-y engages; set to auto to restore the old grow-past-the-cap behavior. |
--modal-display |
flex |
display | Display property of the scrollable content area. |
--modal-overflow-y |
scroll |
overflow-y | Vertical overflow behavior of the content area. |
--modal-scrollbar-width |
none |
scrollbar-width | Scrollbar width for the content area (set 'none' to hide). |
--modal-content-padding |
0 |
padding | Padding inside the modal content area (slot-content). Defaults to 0; set to inset the body from the modal walls. |
--modal-content-open-transition-duration |
380ms (fly) / 300ms (fade) |
transform, opacity | Duration of the content panel's entrance transition — fly for align='top'/'bottom' (or entryAnimation='slide-up'/'slide-down'), fade for align='center'. Falls back through --motion-duration. Full detail in ModalAnimation.md. |
--modal-content-close-transition-duration |
380ms (fly) / 300ms (fade) |
transform, opacity | Duration of the content panel's exit transition (only when transitionType='ALL'). Same fallback as the open duration token above. Full detail in ModalAnimation.md. |
--modal-content-open-transition-distance |
60px |
transform | Distance (px) the fly-shaped entrance travels; not read by the fade shape. Falls back through --distance-overlay. Full detail in ModalAnimation.md. |
--modal-content-close-transition-distance |
60px |
transform | Distance (px) the fly-shaped exit travels. Same fallback as the open distance token above. |
--modal-content-open-transition-easing |
cubicOut (fly) / linear (fade) |
transform, opacity | Easing of the content panel's entrance transition, parsed from a CSS easing keyword or cubic-bezier(...). Falls back through --ease-smooth-out, then --motion-easing. Full detail in ModalAnimation.md. |
--modal-content-close-transition-easing |
cubicOut (fly) / linear (fade) |
transform, opacity | Easing of the content panel's exit transition. Same fallback as the open easing token above. |
--modal-center-justify-content |
center |
justify-content | Vertical positioning of the modal when align='center'. |
--modal-center-align-items |
center |
align-items | Horizontal positioning of the modal when align='center'. |
--modal-bottom-justify-content |
flex-end |
justify-content | Vertical positioning of the modal when align='bottom'. |
--modal-bottom-align-items |
- |
align-items | Horizontal positioning of the modal when align='bottom'. |
--modal-top-justify-content |
flex-start |
justify-content | Vertical positioning of the modal when align='top'. |
--modal-top-align-items |
- |
align-items | Horizontal positioning of the modal when align='top'. |
--modal-center-max-width |
calc(100vw - var(--modal-viewport-gutter, 32px)) |
max-width | Width ceiling of the content panel when align='center', applied on top of every size class and every width token; a percentage token resolves against the same box it did before the ceiling existed, except beside content wider than the viewport (see Viewport fit). Set none to remove it. A host that embeds the modal in a region narrower than the viewport sets this to that region's width. |
--modal-top-max-width |
100vw |
max-width | Width ceiling of the content panel when align='top'. Edge to edge by default, with no gutter. |
--modal-bottom-max-width |
100vw |
max-width | Width ceiling of the content panel when align='bottom'. Separate from the centered token so an app-wide default for dialogs never reshapes bottom sheets. |
--modal-small-height |
20vh |
height | Height of the modal content when size='small'. |
--modal-small-width |
- |
width | Width of the modal content when size='small'. Capped by the alignment's max-width ceiling. |
--modal-medium-height |
50vh |
height | Height of the modal content when size='medium'. |
--modal-medium-width |
- |
width | Width of the modal content when size='medium'. Capped by the alignment's max-width ceiling. |
--modal-large-height |
80vh |
height | Height of the modal content when size='large'. |
--modal-large-width |
- |
width | Width of the modal content when size='large'. Capped by the alignment's max-width ceiling. |
--modal-fit-content-max-height |
80vh |
max-height | Maximum height of the modal when size='fit-content'. |
--modal-header-background-color |
#f6f7f9 |
background-color | Background color of the modal header bar. |
--modal-header-padding |
18px 20px |
padding | Padding inside the header bar. |
--modal-header-border-radius |
0px |
border-radius | Corner rounding of the header bar. |
--modal-header-border-bottom |
none |
border-bottom | Bottom border of the header bar. |
--modal-header-align-items |
center |
align-items | Vertical alignment of items inside the header bar. |
--modal-footer-background-color |
#f6f7f9 |
background-color | Background color of the footer area. |
--modal-footer-padding |
18px 20px |
padding | Padding inside the footer area. |
--modal-footer-border-radius |
0px |
border-radius | Corner rounding of the footer area. |
--modal-footer-border-top |
none |
border-top | Top border of the footer area. |
--modal-footer-justify-content |
none |
justify-content | Horizontal alignment of footer content. |
--modal-footer-gap |
0px |
gap | Gap between primary and secondary footer buttons, and between rows when they wrap. |
--modal-footer-action-buttons-width |
fit-content |
width | Width of the footer action buttons container. |
--modal-footer-action-buttons-justify-content |
flex-start |
justify-content | Horizontal alignment of the footer action buttons, which wrap onto a second row when they do not fit. Not inherited from --modal-footer-justify-content. |
--modal-footer-secondary-button-max-height |
- |
--button-max-height | Maximum height of the secondary footer button. |
--modal-footer-secondary-button-max-width |
- |
--button-max-width | Maximum width of the secondary footer button. |
--modal-footer-secondary-button-font-family |
- |
--button-font-family | Font family of the secondary footer button. |
--modal-footer-secondary-button-font-weight |
500 |
--button-font-weight | Font weight of the secondary footer button. |
--modal-footer-secondary-button-font-size |
14px |
--button-font-size | Font size of the secondary footer button. |
--modal-footer-secondary-button-color |
#3a4550 |
--button-color | Background color of the secondary footer button. |
--modal-footer-secondary-button-text-color |
white |
--button-text-color | Text color of the secondary footer button. |
--modal-footer-secondary-button-height |
fit-content |
--button-height | Height of the secondary footer button. |
--modal-footer-secondary-button-padding |
16px |
--button-padding | Padding inside the secondary footer button. |
--modal-footer-secondary-button-margin |
- |
--button-margin | Margin around the secondary footer button. |
--modal-footer-secondary-button-border-radius |
0px |
--button-border-radius | Corner rounding of the secondary footer button. |
--modal-footer-secondary-button-width |
fit-content |
--button-width | Width of the secondary footer button. |
--modal-footer-secondary-button-cursor |
pointer |
--cursor | Cursor style of the secondary footer button. |
--modal-footer-secondary-button-opacity |
1 |
--opacity | Opacity of the secondary footer button. |
--modal-footer-secondary-button-border |
none |
--button-border | Border of the secondary footer button. |
--modal-footer-secondary-button-justify-content |
center |
--button-justify-content | Justify content of the secondary footer button. |
--modal-footer-secondary-button-content-gap |
16px |
--button-content-gap | Gap between content elements in the secondary footer button. |
--modal-footer-secondary-button-visibility |
visible |
--button-visibility | Visibility of the secondary footer button. |
--modal-footer-secondary-button-box-shadow |
none |
--button-box-shadow | Box shadow of the secondary footer button. |
--modal-footer-secondary-button-content-flex-direction |
row |
--button-content-flex-direction | Flex direction of the secondary footer button content. |
--modal-footer-secondary-button-disabled-color |
- |
--disabled-background-color | Background color of the secondary footer button when disabled. |
--modal-footer-secondary-button-disabled-text-color |
- |
--disabled-text-color | Text color of the secondary footer button when disabled. |
--modal-footer-secondary-button-disabled-border |
- |
--disabled-border | Border of the secondary footer button when disabled. |
--modal-footer-secondary-button-disabled-opacity |
0.4 |
--disabled-opacity | Opacity of the secondary footer button when disabled. |
--modal-secondary-button-order |
none |
order | Flex order of the secondary footer button. |
--modal-footer-secondary-button-flex-value |
none |
flex | Flex value of the secondary footer button. |
--modal-footer-primary-button-max-height |
- |
--button-max-height | Maximum height of the primary footer button. |
--modal-footer-primary-button-max-width |
- |
--button-max-width | Maximum width of the primary footer button. |
--modal-footer-primary-button-font-family |
- |
--button-font-family | Font family of the primary footer button. |
--modal-footer-primary-button-font-weight |
500 |
--button-font-weight | Font weight of the primary footer button. |
--modal-footer-primary-button-font-size |
14px |
--button-font-size | Font size of the primary footer button. |
--modal-footer-primary-button-color |
#3a4550 |
--button-color | Background color of the primary footer button. |
--modal-footer-primary-button-text-color |
white |
--button-text-color | Text color of the primary footer button. |
--modal-footer-primary-button-height |
fit-content |
--button-height | Height of the primary footer button. |
--modal-footer-primary-button-padding |
16px |
--button-padding | Padding inside the primary footer button. |
--modal-footer-primary-button-margin |
- |
--button-margin | Margin around the primary footer button. |
--modal-footer-primary-button-border-radius |
0px |
--button-border-radius | Corner rounding of the primary footer button. |
--modal-footer-primary-button-width |
fit-content |
--button-width | Width of the primary footer button. |
--modal-footer-primary-button-cursor |
pointer |
--cursor | Cursor style of the primary footer button. |
--modal-footer-primary-button-opacity |
1 |
--opacity | Opacity of the primary footer button. |
--modal-footer-primary-button-border |
none |
--button-border | Border of the primary footer button. |
--modal-footer-primary-button-justify-content |
center |
--button-justify-content | Justify content of the primary footer button. |
--modal-footer-primary-button-content-flex-direction |
row |
--button-content-flex-direction | Flex direction of the primary footer button content. |
--modal-footer-primary-button-content-gap |
16px |
--button-content-gap | Gap between content elements in the primary footer button. |
--modal-footer-primary-button-disabled-color |
- |
--disabled-background-color | Background color of the primary footer button when disabled. |
--modal-footer-primary-button-disabled-text-color |
- |
--disabled-text-color | Text color of the primary footer button when disabled. |
--modal-footer-primary-button-disabled-border |
- |
--disabled-border | Border of the primary footer button when disabled. |
--modal-footer-primary-button-disabled-opacity |
0.4 |
--disabled-opacity | Opacity of the primary footer button when disabled. |
--modal-footer-primary-button-visibility |
visible |
--button-visibility | Visibility of the primary footer button. |
--modal-footer-primary-button-box-shadow |
none |
--button-box-shadow | Box shadow of the primary footer button. |
--modal-primary-button-order |
none |
order | Flex order of the primary footer button. |
--modal-footer-primary-button-flex-value |
none |
flex | Flex value of the primary footer button. |
--header-text-size |
16px |
font-size | Font size of the header title text. |
--modal-header-text-weight |
- |
font-weight | Font weight of the header title text. |
--modal-header-text-line-height |
- |
line-height | Line height of the header title text. |
--modal-header-text-letter-spacing |
- |
letter-spacing | Letter spacing of the header title text. |
--header-img-top-padding |
5px |
padding-top | Top padding on both header images. |
--header-left-image-margin |
0px 18px 0px 0px |
margin | Margin around the left header image. |
--header-left-image-width |
25px |
width | Width of the left header image. |
--header-left-image-height |
25px |
height | Height of the left header image. |
--header-right-image-width |
25px |
width | Width of the right header image. |
--header-right-image-height |
25px |
height | Height of the right header image. |
--header-right-image-padding |
- |
padding | Padding around the right header image. |
Viewport fit
The content panel is capped to the viewport on both axes, so a width token, a wide child or a long footer label cannot push the dialog off-screen.
- Width. A centered dialog is capped at
100vwminus--modal-viewport-gutter(32px by default, 16px each side).align="top"andalign="bottom"are capped at100vwbecause a sheet runs edge to edge.--modal-small-width,--modal-medium-widthand--modal-large-widthstill win until they exceed the ceiling, and a keyword such asfit-contentorautokeeps working. A percentage width resolves against the same box it did before the ceiling existed, socalc(100% - 32px)is still 343px on a 375px phone, with or withoutenableTransition. The exception is a percentage token beside content that cannot shrink below the viewport width, such as an unbreakable 700px child: the transition wrapper is now bounded to the viewport, so the percentage is taken of the screen instead of the oversized content (90%is 337.5px at 375px, where it used to be 658.8px and ran off the screen). With the transition wrapper, a capped panel is centered, but a percentage panel narrower than the ceiling is offset from the start edge by half the gutter (16px at 375px) instead of sitting flush with it, so it is not centered;enableTransition={false}centers it. Replace a ceiling with--modal-center-max-width,--modal-top-max-widthor--modal-bottom-max-width;nonerestores the old unbounded width. A body wider than the capped panel scrolls inside it and the footer stays put. - Height. For
sizesmall, medium and large,--modal-max-heightdefaults to100dvhminus the same gutter token, so one token governs both axes.size="fit-content"is bounded by--modal-fit-content-max-height(80vh) instead, which the gutter does not change. A unitless--modal-viewport-gutter: 0drops the height cap along with the width cap; write0px. - Footer. The buttons built from the
footerprop wrap onto a second row when they do not fit.--modal-footer-gapis the gap between rows as well as between buttons (its default is0px, so set it if wrapped buttons should not touch), and--modal-footer-action-buttons-justify-contentaligns the rows. A single label wider than the panel still overflows it and scrolls with the panel, so keep footer labels short. - Embedding. The ceilings are measured against the viewport. A host that shrinks the overlay with
--modal-widthor--modal-margin, or mounts it in a narrower region, should set--modal-center-max-widthto that region's width. - Where to set the tokens. The ceiling is chosen on the overlay element, so set these tokens on the overlay (through
classes) or on an ancestor of it rather than on.modal-content. WithusePortalthe overlay is moved todocument.body(or the shadow root), so an ancestor of where the modal was written no longer reaches it: useclasses, or set the tokens on an ancestor of the portal target. - Clicks beside the panel. The transition wrapper that animates the panel no longer takes pointer events, so the area beside the panel that it covers counts as the overlay: a click there fires
onoverlayclickandondismiss, as a click anywhere else outside the panel does. That area appears where a percentage token leaves the panel narrower than its wrapper, where a ceiling caps the panel, and beside everyalign="top"oralign="bottom"panel that is narrower than the viewport, because the wrapper stretches across the sheet by default; a click there used to land on the wrapper and do nothing. A drag that starts inside the panel, a text selection for instance, and is released beside it also dismisses in Chromium and WebKit, which send the click to the nearest common ancestor (Firefox sends none); a drag released on the far overlay already did. A click inside the panel never dismisses. Setpointer-events: autoon.modal-animationto restore the old behaviour. - Overriding the panel. Every rule the library adds for the viewport fit has a specificity of zero: the panel's
max-widthceiling and itsmargin-inline-startinside the transition wrapper, the wrapper's ownpointer-eventsandmax-width, and the footer row'sflex-wrapandjustify-content. A rule of your own on.modal-content,.modal-animationor.footer-action-buttonstherefore wins whatever its selector and whichever stylesheet loads first:.modal-content { max-width: 600px },.modal-content { margin: 0 auto }and.modal-content .footer-action-buttons { justify-content: flex-end }all take effect. Amax-widthof your own replaces the ceiling, so it has to fit the viewport itself. A universal*rule that comes later in the cascade ties with these rules and wins.
Type Reference
Custom types used by this component's props and events:
ModalSize
type ModalSize = 'large' | 'medium' | 'small' | 'fit-content';
ModalAlign
type ModalAlign = 'top' | 'center' | 'bottom';
ModalTransition
type ModalTransition = 'IN' | 'ALL';
ModalEntryAnimation
type ModalEntryAnimation = 'fade' | 'slide-up' | 'slide-down';
ModalRole
type ModalRole = 'dialog' | 'alertdialog';
ButtonProperties
type ButtonProperties = {
text?: string;
enable?: boolean;
showProgressBar?: boolean;
showLoader?: boolean;
loaderType?: 'Circular' | 'ProgressBar';
type?: 'submit' | 'reset' | 'button';
testId?: string;
icon?: Snippet;
children?: Snippet;
ariaLabel?: string;
ariaExpanded?: boolean;
disabled?: boolean;
classes?: string;
onclick?: (event: MouseEvent) => void;
onkeyup?: (event: KeyboardEvent) => void;
};
Internal Dependencies
This component uses the following library components internally:
- Animations
- Button
Web Component
Tag: <sui-modal>
<sui-modal size="medium" show-overlay>
<p>Modal body content</p>
<div slot="footer">
<button>Cancel</button>
<button>Confirm</button>
</div>
</sui-modal>
aria-label and role are not props on <sui-modal> — they're reserved accessors on every custom element (ARIAMixin/role), so declaring them as component props would replace the native ones instead of adding to them. Setting them as native attributes directly on the host (<sui-modal role="alertdialog" aria-label="Delete account">) does not name the dialog: it lands on the host element, which the shadow tree wraps around its whole full-screen overlay, not on the .modal-content panel that actually carries the accessible name and aria-modal. Use modal-role / modal-aria-label (properties modalRole / modalAriaLabel) instead — same prefixed-alias pattern as <sui-toggle>'s input-aria-label — which forward to the panel's role/aria-label:
<sui-modal modal-role="alertdialog" modal-aria-label="Delete account">
<p>This can't be undone.</p>
</sui-modal>
The host's own role/aria-label stay free for whatever the page around the modal wants to do with them.
ariaDescribedby (attribute aria-describedby) is an ordinary prop on <sui-modal>: it is not an ARIAMixin accessor (only ariaDescribedByElements and ariaDescription are), so it needs no alias. An id reference is resolved inside the tree that contains the referencing element, and the panel lives in the shadow root, so it can only describe elements that are also in the shadow root: content supplied through the content snippet property. Light-DOM content passed through the default <slot> sits outside the shadow root and an id cannot reach it, so a plain-HTML consumer who only uses slots cannot describe the panel this way. overlayAriaLabel is an ordinary prop and works the same way as on the Svelte component.
The viewport ceilings above apply to <sui-modal> unchanged. The default footer it builds from the footer property only half applies: the footer container tokens (--modal-footer-background-color, --modal-footer-padding, --modal-footer-border-top, --modal-footer-border-radius, --modal-footer-justify-content) reach it, because that container is rendered by Modal.svelte. The action row and the buttons are not: the wrapper creates them in its own template, outside the Svelte-scoped footer rules in Modal.svelte, so --modal-footer-gap, --modal-footer-action-buttons-width, the wrapping, --modal-footer-action-buttons-justify-content and every --modal-footer-primary-button-* and --modal-footer-secondary-button-* token do not. Supply your own markup through the footer slot when the action row or the buttons need that styling.
onheaderrightimageclick, onheaderleftimageclick, onprimarybuttonclick, onsecondarybuttonclick, onoverlayclick, and ondismiss also dispatch same-named DOM custom events (bubbles, composed) for a consumer who only calls addEventListener — the four click ones (headerrightimageclick, headerleftimageclick, primarybuttonclick, secondarybuttonclick) carry detail set to the same MouseEvent the property callback receives; overlayclick and dismiss carry no detail. onclose and onkeydown do not: both are already HTMLElement's own native events, so modal.addEventListener('close', ...) and modal.addEventListener('keydown', ...) register without error but are never called by this component — assign the properties instead.
Slots
| Slot Name | Maps to Snippet | Description |
|---|---|---|
| (default) | content |
The main body content of the modal. |
footer |
footerSnippet |
Content rendered in the modal footer area; defaults to the primary/secondary buttons built from the footer prop. |
Embedded scroll ownership
scrollContainer?: ScrollContainer selects the element whose scrolling this overlay owns.
Omit it for the overlay's ownerDocument.body (the standalone default). Pass an
HTMLElement, or a getter such as scrollContainer={() => rootElement} when the
application binds its scroll root after initialization. An explicit null, or a
getter returning null, acquires no lock and never falls back to the host body.
The element must be the actual scrolling container, which may be an element
inside an application shadow root; a ShadowRoot itself does not scroll.
Ownership is resolved once per open span. A changed prop/getter takes effect on
the next open; closing or unmounting always releases the element acquired when
opening. Nested Modal, Sheet and CommandMenu instances share reference counting
per element, including legacy body locks. The last release restores the original
overflow shorthand/longhands and their CSS priorities while preserving scroll
position and unrelated inline declarations. Focus and dismissal behavior are
unchanged. Scroll ownership does not select a portal destination; embedded Modal
callers should keep the default usePortal={false}, or retain their shadow-root
portal containment.
<script lang="ts">
import { Modal, type ScrollContainer } from '@juspay/svelte-ui-components';
let rootElement: HTMLDivElement | null = $state(null);
const scrollContainer: ScrollContainer = () => rootElement;
</script>
<div bind:this={rootElement} style="height: 100%; overflow: auto">
<Modal {scrollContainer}>
{#snippet content()}<p>Owned dialog content</p>{/snippet}
</Modal>
</div>
The package also exports acquireScrollLock(element) for custom overlays. It
returns an idempotent disposer; retain that disposer and invoke it on teardown.
For the custom element, assign element.scrollContainer = rootElement (or a
getter) as a JavaScript property before opening/mounting. No selector string
or serialized HTML attribute API is supported. Invalid non-element property
values are ignored without falling back to the host body.
Focus containment
containFocus (default false) brings programmatic/background focus back to the
modal while it owns the foreground. It yields to later registered library popups
and native Dialog surfaces, and removes its listener on unmount. It complements
the existing Tab trap without forcing a new policy on existing consumers.
Focus restoration
Omitting restoreFocusTo keeps the existing captured-opener restoration unchanged.
To return to a different owned control, pass an element or a getter:
<Modal restoreFocusTo={() => currentLabelInput} {content} />
The option's value or function reference is captured once when the Modal mounts.
Changing the prop while that instance is mounted does not replace its return
owner; use a new mount for a new presentation. A getter reads the current target
synchronously once at actual teardown, after focus/dismissal listeners,
scroll locking and hardware-back cleanup are released. Close-intent callbacks
such as onclose, onoverlayclick and ondismiss retain their current timing;
they are not after-close hooks. An outro can retain the Modal after intent, so
neither tick() nor a guessed delay establishes this restoration point.
Explicit null, a getter returning null or throwing, and disconnected,
non-HTML or foreign-document targets suppress restoration without falling back
to the old opener. A target must be connected in the Modal's captured owning
document; elements within its shadow trees are supported. Native Node brand and
composed-root checks accept genuine HTML targets across JavaScript realms,
including adopted elements, without accepting plain object lookalikes. The
existing .focus() call semantics are unchanged.
The consumer owns generation, route, app-root and intentional-focus fences, and
must return only an enabled, visible, non-inert target in its current context.
A getter must only select an element or return null: do not call focus(),
mutate business state, or schedule another focus operation inside it.
For the custom element, assign the Object property before the mounted open:
modal.restoreFocusTo = () => currentLabelInput; // or an HTMLElement / null
This is not a selector-string attribute. There is no new after-close event, focus timer or automatic root/route ownership policy.