Modal3.0
Installation
npm i @tryg/vue-ui-libraryUsage
Install the component library as a Vue plugin. This globally registers all Anchor components.
import { createApp } from 'vue';
import { ComponentLibrary } from '@tryg/vue-ui-library';
import App from './App.vue';
const app = createApp(App);
app.use(ComponentLibrary);
app.mount('#app');<script setup>
import { ref } from 'vue';
const isOpen = ref(false);
</script>
<template>
<anchor-modal :is-visible="isOpen">ANYTHING HERE IS SHOWN WHEN THE MODAL OPENS</anchor-modal>
<anchor-button text="OPEN MODAL" variant="primary" @click="isOpen = true"></anchor-button>
</template>Import only the components you need for optimal bundle size.
<script setup>
import { ref } from 'vue';
import { AnchorModal, AnchorButton } from '@tryg/vue-ui-library';
const isOpen = ref(false);
</script>
<template>
<anchor-modal :is-visible="isOpen">ANYTHING HERE IS SHOWN WHEN THE MODAL OPENS</anchor-modal>
<anchor-button text="OPEN MODAL" variant="primary" @click="isOpen = true"></anchor-button>
</template>Modal button slots
You have to specify the text you want shown next to the Close icon. You add the text as a slot and use the slot name to control if the text is shown before or after the Close icon.
Like this:
<template>
<anchor-modal>
<span slot="close-button-before-icon">Close</span>
</anchor-modal>
</template>or
<template>
<anchor-modal>
<span slot="close-button-after-icon">Close</span>
</anchor-modal>
</template>CSS variables
The modal is by default set to be 600px wide and 100% minus a padding for the height. Both of these values are exposed as CSS variables and can be changed in your stylesheet.
| Variable | Description |
|---|---|
--height | DEPRECATED: kept for backwards compatibility, but will be removed by April 2024 |
--modal-border-radius | Sets the radius for the corners of the Modal |
--modal-font-family | Allows you to set a specific font-family for the Modal |
--modal-header-height | Sets the height of the header of the Modal |
--modal-height | The height of the Modal. Set to 100% of its parent container |
--modal-margin-horizontal | The space reserved to the left and right of the Modal |
--modal-margin-vertical | The space reserved above and below the Modal |
--modal-padding | The padding of the content area |
--modal-position | Determines the position value of the anchor-modal and anchor-modal__overlay classes |
--modal-width | The width of the Modal. Set to 600 pixels |
--modal-z-index | The z-index of the Modal, when opened |
--width | DEPRECATED: kept for backwards compatibility, but will be removed by April 2024 |
Set a custom width and height for the Modal by setting your own values for these CSS variables:
anchor-modal {
--modal-width: 400px;
--modal-height: 400px;
}Modal without close button
<script setup>
import { ref } from 'vue';
const isOpen = ref(false);
</script>
<template>
<anchor-modal :is-visible="isOpen" close-btn="false">CONTENT</anchor-modal>
<anchor-button text="OPEN MODAL" variant="primary" @click="isOpen = true"></anchor-button>
</template>Modal is open from the beginning
<template>
<anchor-modal is-visible="true">CONTENT</anchor-modal>
</template>Accessibility
- The modal uses
role="dialog"andaria-modal="true"to clearly communicate its purpose to assistive technologies. - Focus is trapped within the modal while it is open using the
focusTrapprop (enabled by default), preventing users from tabbing into background content. - Pressing Escape closes the modal, providing a standard keyboard escape mechanism.
- The close button receives an
aria-labelfrom the requiredcloseBtnTitleprop, ensuring screen readers announce a meaningful action. - Background scrolling is locked when the modal is open via the
scrollLockprop (enabled by default). The overlay behind the modal hasaria-hidden="true"to avoid announcing decorative content.
API
anchor-modal
Properties
| Property | Attribute | Description | Type | Default |
|---|---|---|---|---|
closeBtn | close-btn | Determines whether the close button should be displayed in the modal. If true, the close button will be shown. If false, the close button will be hidden. The default value is true. | boolean | true |
closeBtnTitle (required) | close-btn-title | Set a text to use as the title and aria-label for close button. The text will be shown on hover | string | undefined |
focusTrap | focus-trap | Determines whether the focus should be trapped within the modal when it is open. If true, the focus will be trapped. If false, the focus will not be trapped. The default value is true. | boolean | true |
isVisible | is-visible | Determines whether the modal is visible or hidden. If true, the modal will be visible. If false, the modal will be hidden. The default value is false. | "false" | "true" | 'false' |
scrollLock | scroll-lock | Determines whether the scrolling of the background content should be locked when the modal is open. If true, the scrolling will be locked. If false, the scrolling will not be locked. The default value is true. | boolean | true |
uuid | uuid | A unique identifier for the modal component. It is used to set the id attribute of the modal element. The default value is generated using the generateGuid() function. | string | `anchor-modal-${uuidv4()}` |
Events
| Event | Description | Type |
|---|---|---|
closed | Emitted when the modal is closed. The event data is set to true. | CustomEvent<any> |
opened | Emitted when the modal is opened. The event data is set to true. | CustomEvent<any> |
Methods
close() => Promise<void>
Closes the modal. It removes the scroll lock, hides the modal, and emits the closed event with a value of true.
Returns
Type: Promise<void>
open() => Promise<void>
Opens the modal. It shows the modal, emits the opened event with a value of true, and sets the scroll lock if enabled.
Returns
Type: Promise<void>
CSS Custom Properties
| Name | Description |
|---|---|
--height | DEPRECATED: kept for backwards compatibility, but will be removed by April 2024 |
--modal-border-radius | This sets the radius for the corners of the Modal |
--modal-font-family | This allows you to set a specific font-family for the Modal |
--modal-header-height | This sets the height of the header of the Modal |
--modal-height | The height of the Modal. It is set to 100% of its parent container. |
--modal-margin-horizontal | The space reserved to the left and right of the Modal |
--modal-margin-vertical | The space reserved above and below the Modal |
--modal-padding | The padding of the content area |
--modal-position | Determines the position value of the anchor-modal and anchor-modal__overlay classes |
--modal-width | The width of the Modal. It is set to 600 pixels. |
--modal-z-index | The z-index of the Modal, when opened |
--width | DEPRECATED: kept for backwards compatibility, but will be removed by April 2024 |
Dependencies
Depends on
Graph
graph TD;
anchor-modal --> anchor-icon
style anchor-modal fill:#f9f,stroke:#333,stroke-width:4pxBuilt with StencilJS
