
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
native-modal
Advanced tools
<script lang="ts" setup> import { useData } from 'vitepress' import { initNativeModal } from '../src/lib/index.js' import {computed} from ".vitepress/cache/deps/vue.js"; initNativeModal()
Package is available on npm and can be installed from the command line.
$ npm i native-modal
You can also download or link to the latest compiled version using the CDN.
https://unpkg.com/native-modal/dist/native-modal.min.js
See simple usage with attribute based modals.
import { initNativeModal } from 'native-modal'
initNativeModal() // register attrs handling
<!-- pass `dialog` selector in attr -->
<button data-modal-open="dialog">Show modal</button>
...
<dialog>
<h2>NativeModal</h2>
<p>Hello, click on OK to close modal.</p>
<button data-modal-close>OK</button>
</dialog>
Hello, click on OK to close modal.
OK::: tip Appearance The original modal window doesn't have any styles, so you'll need to style the dialog box yourself or use ready-made themes, or even write your own theme. :::
<button data-modal-open="#first">Open first</button>
<button data-modal-open="#second">Open second</button>
...
<dialog id="first">
<h2>First!</h2>
<p>Hello, click on OK to close modal.</p>
<button data-modal-close>OK</button>
</dialog>
<dialog id="second">
<h2>I am second</h2>
<p>Hello, click on OK to close modal.</p>
<button data-modal-close>OK</button>
</dialog>
Hello, click on OK to close modal.
OKHello, click on OK to close modal.
OKYou can modify the behavior of the modal window by passing parameters to the dialog box. It's very simple. Let's look at an examples:
esc
closingBy default, an open dialog box can be closed using esc
.
We can prevent this by passing the parameter disable-esc
.
<button data-modal-open="dialog">Open modal</button>
...
<dialog disable-esc>
<h2>Hello</h2>
<p>Pressing <code>esc</code> has no effect</p>
<button data-modal-close>Good</button>
</dialog>
esc
Pressing esc
has no effect
You can set a props once
so that the modal window is triggered only once.
<button data-modal-open="dialog">Open modal</button>
...
<dialog once>
<h2>Onetime</h2>
<p>You'll only see me once, thank attribute <code>once</code>. </p>
<button data-modal-close>Thx for once</button>
</dialog>
You'll only see me once, thank attribute once
.
Add delay to opening modal via show-delay
<button data-modal-open="dialog">Open modal</button>
...
<dialog disable-esc show-delay="2000">
<h2>Hello</h2>
<p>Also pressing <code>esc</code> has no effect</p>
<button data-modal-close>Cool</button>
</dialog>
Modal opened after 2000ms
Also pressing esc
has no effect
To control modal windows from a script, you can use the Modal
class.
First, we need to define the modal structure of the DOM
<dialog id="modal">
<h2>Modal</h2>
<p>Hello, click on OK to close modal.</p>
<button data-modal-close>OK</button>
</dialog>
Next, we need to create a Modal instance:
import { Modal } from 'native-modal'
const modal = new Modal('#modal')
modal.open() // Yeah, you open me!
modal.close() // easy to close!
Okay, how do I apply the parameters? Let's look at an example:
import { Modal } from 'native-modal'
const modal = new Modal('#modal', {
animation: true, // enable open/close animation
disableEsc: true, // prevent esc closing
})
modal.open() // Well done!
You can easily customize the content of your modal window, this package gives you a simple wrapper to work with your modal window, its appearance is solely your responsibility.
FAQs
NativeModal - tiny wrapper over the <dialog />. No overhead scripts. Just modal.
The npm package native-modal receives a total of 0 weekly downloads. As such, native-modal popularity was classified as not popular.
We found that native-modal demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.