svelte-dialog-component
v0.0.1
Published
## Installation
Downloads
1
Readme
svelte-dialog-component
Installation
- Ensure you have node installed on your OS (v19 and above - recommended)
- Navigate to the app where you would like to use the component and run the following in your terminal
npm install svelte-dialog-component --save
How to use the component
- Inside the script tag of your .svelte file
import { Dialog } from 'svelte-dialog-component'
- Inside an HTML element use the imported Menu component like so
<Dialog
bind:dialog
headerText={"Your header text"}
{footerButtons}
on:footerButtonHandler={handleFooterButton}
on:escapePressed={handleEscape}
>
Some text or html here that will be passed in via a slot
</Dialog>
Props, handlers
bind:dialog
RequiredheaderText={"Your header text"}
Optional, String to display as header text{footerButtons}
Optional, Array of objectson:footerButtonHandler={handleFooterButton}
Optional, Function to handle te events emited by the footerButtonson:escapePressed={handleEscape}
Optional, a function to handle the behaviour of the dialog when the Escape key is pressed
Example
+layout.svelte
<script>
import { onMount } from "svelte";
import Dialog from "$lib/Dialog/Dialog.svelte";
import {tick} from 'svelte';
/**
* @type {{ showModal: () => void; close: () => void; }}
*/
let dialog;
// Declaring the variable(s) used to show and hide the dialog
let showDialog = false;
// Declaring a list of buttons that will appear in the footer of the dialog
let footerButtons = [
{label: "Cancel", code: "cancel"},
{label: "Clear", code: "clear"},
{label: "Submit", code: "submit"}
]
// A function that will show the dialog
async function openDialog() {
showDialog = true;
await tick(); // waits for dialog to be set
dialog.showModal();
}
// A function that will hide the dialog
function hideDialog() {
dialog.close();
showDialog = false;
}
/**
* A function to handel all the footerButtons for the dialog
* @param {{ detail: { code: string; }; }} e
*/
function handleFooterButton(e) {
if(e.detail && e.detail.code === 'cancel') {
// handle cancel
}
if(e.detail && e.detail.code === 'clear') {
// handle clear
}
if(e.detail && e.detail.code === 'submit') {
// handle submit
}
hideDialog();
}
/**
* A function to handel escape key pressed
*/
function handleEscape() {
// handle escape
hideDialog();
}
</script>
<!-- The imported dialog component -->
{#if showDialog}
<Dialog
bind:dialog
headerText={"Your header text"}
{footerButtons}
on:footerButtonHandler={handleFooterButton}
on:escapePressed={handleEscape}
>
Some text or html here that will be passed in via a slot
</Dialog>
{/if}
<button on:click={openDialog}>Open</button>
Styling
Can be set with variables associated with every element
--dialogContainerBackgroundColor
--dialogHeaderColor
--dialogFooterFirstButtonBackgroundColor
--dialogFooterFirstButtonColor
--dialogFooterFirstButtonHoverBackgroundColor
--dialogFooterButtonBorder
--dialogFooterButtonColor
--dialogFooterButtonHoverBorder
Feedback and recommendations
Please send me feedback or recommendations for improvements at [email protected]. I would love to here from you. Donations are welcome but not necessary.