Modal

The Modal component provides a flexible and customizable way to display overlay content, perfect for dialogs, alerts, or any content that requires user interaction without leaving the current page.

About

Modals are versatile UI elements used to present important information or interactive content in a focused overlay. They help keep users engaged by keeping the main interface visible in the background while drawing attention to the modal content. The Modal component in theui-svelte offers various customization options, including animations, backdrops, and event handling, ensuring it fits seamlessly into your application's design and functionality.

Example

To use the Modal component, you need to import it into your Svelte file. This setup allows you to easily integrate the Modal component into your project.

Svelte
<script>
  import { Modal } from "theui-svelte";
</script>

<Modal label="Modal Button">
  Hello Modal!
</Modal>

Positioning

The position prop in the Modal component determines where the modal is displayed vertically on the screen. It can be set to 'top', 'center', or 'bottom', with the default being 'center'. This allows you to align the modal according to your design needs, whether you want it at the top for quick alerts, in the center for standard dialogs, or at the bottom for additional content.

Svelte
<!-- Top-aligned Modal -->
<Modal position="top"> ... </Modal>

<!-- Centered Modal (Default) -->
<Modal position="center"> ... </Modal>

<!-- Bottom-aligned Modal -->
<Modal position="bottom"> ... </Modal>

Sizing

The size prop controls the width of the Modal. Options include 'sm' for small, 'md' for medium (default), 'lg' for large, and 'full' for full-screen. This allows you to adjust the modal size based on your content needs.

Svelte
<!-- Small Modal -->
<Modal size="sm"> ... </Modal>

<!-- Medium-sized Modal (default) -->
<Modal size="md"> ... </Modal>

<!-- Large Modal -->
<Modal size="lg"> ... </Modal>

<!-- Full-width Modal -->
<Modal size="full"> ... </Modal>

Animation

Animation Type

The animation prop defines the type of animation used when the Modal opens or closes. Available options include 'slide-down', 'slide-up', 'fade', 'zoom-in', and 'zoom-out'. Each option provides a different visual effect for how the Modal appears or disappears.

Svelte
<!-- Modal with slide-down animation -->
<Modal animation="slide-down"> ... </Modal>

<!-- Modal with slide-up animation -->
<Modal animation="slide-up"> ... </Modal>

<!-- Modal with fade (default) animation -->
<Modal animation="fade"> ... </Modal>

<!-- Modal with zoom-in animation -->
<Modal animation="zoom-in"> ... </Modal>

<!-- Modal with zoom-out animation -->
<Modal animation="zoom-out"> ... </Modal>

Animation Speed

The animate prop controls the speed of the Modal's animation. It accepts the following values: false, 'slower', 'slow', 'normal', 'fast', and 'faster'. Setting this prop to false will disable the animation, while the other values adjust the animation speed accordingly.

Svelte
<!-- Modal with slower animation -->
<Modal animate="slower"> ... </Modal>

<!-- Modal with slow animation -->
<Modal animate="slow"> ... </Modal>

<!-- Modal with normal speed (default) animation -->
<Modal animate="normal"> ... </Modal>

<!-- Modal with fast animation -->
<Modal animate="fast"> ... </Modal>

<!-- Modal with faster animation -->
<Modal animate="faster"> ... </Modal>

<!-- Modal with no animation -->
<Modal animate={false}> ... </Modal>

Disable Close Button

The closeButton prop allows you to control the visibility of the close button in the Modal. By default, the close button is hidden (false). Set this prop to true to display the close button, giving users an easy way to close the modal.

Svelte
<Modal closeButton={false}> ... </Modal>

Customization

To create a custom-styled modal, you can use the following props to apply custom classes to the modal's header, body, footer, and outer container:

  • modalHeaderClasses: Customize the header section of the modal.
  • modalBodyClasses: Customize the body section of the modal.
  • modalFooterClasses: Customize the footer section of the modal.

These props allow you to apply your own styles or use TailwindCSS classes to control the layout and appearance of different parts of the modal.

Svelte
<Modal
  modalBodyClasses="bg-blue-100 dark:bg-blue-950 text-blue-800 dark:text-blue-300 border-blue-500/50 border-4 py-8"
  modalHeaderClasses="bg-blue-400/50 text-blue-600 dark:text-blue-100 shadow-blue-500/50 rounded-md p-4 border-0 shadow-md text-lg font-bold"
  modalFooterClasses="border-blue-300/50 border rounded-xl p-2"
>
  {#snippet label()}
    <Button label="Modal Button" />
  {/snippet}

  {#snippet header()}
    <h4>Custom Modal Header</h4>
  {/snippet}

  .....

  {#snippet footer()}
    <Button class="bg-warning-500 hover:bg-warning-600 text-warning-900" size="sm">Close</Button>
  {/snippet}
</Modal>

Accessibility

The modal component is designed with accessibility in mind, ensuring it can be used by everyone, including users relying on assistive technologies. By default, it includes the necessary ARIA attributes and supports keyboard navigation.

  1. ARIA Attributes: The Modal uses role="dialog" by default to indicate its purpose to assistive technologies. You can add aria-labelledby and aria-describedby attributes to associate the title and content with the Modal.
  2. Keyboard Navigation
    • Focus is automatically moved to the Modal when it opens.
    • Users can navigate through focusable elements using the tab key.
    • Pressing the Esc key closes the Modal.
  3. Focus Management: Focus is trapped within the Modal while it is open, ensuring users cannot interact with elements outside of it. When the Modal is closed, focus returns to the triggering element.
  4. Screen Reader Announcements: The Modal is announced as a dialog to screen readers, helping users understand its purpose.
  5. Color Contrast: Ensure that text and background colors inside the Modal meet WCAG contrast ratio standards .

Configuration