All Projects → flekschas → Svelte Simple Modal

flekschas / Svelte Simple Modal

Licence: mit
A simple, small, and content-agnostic modal for Svelte v3

Projects that are alternatives of or similar to Svelte Simple Modal

react-spring-bottom-sheet
Accessible ♿️, Delightful ✨, & Fast 🚀
Stars: ✭ 604 (+364.62%)
Mutual labels:  modal, dialog, popup
Jbox
jBox is a jQuery plugin that makes it easy to create customizable tooltips, modal windows, image galleries and more.
Stars: ✭ 1,251 (+862.31%)
Mutual labels:  dialog, modal, popup
vue-modal
A customizable, stackable, and lightweight modal component for Vue.
Stars: ✭ 96 (-26.15%)
Mutual labels:  modal, dialog, popup
LSDialogViewController
Custom Dialog for iOS written in Swift
Stars: ✭ 74 (-43.08%)
Mutual labels:  modal, dialog, popup
plain-modal
The simple library for customizable modal window.
Stars: ✭ 21 (-83.85%)
Mutual labels:  modal, dialog, popup
vue-modal
Reusable Modal component, supports own custom HTML, text and classes.
Stars: ✭ 29 (-77.69%)
Mutual labels:  modal, dialog, popup
React Poppop
A mobile support and multi-directional modal for ReactJS
Stars: ✭ 78 (-40%)
Mutual labels:  dialog, modal, popup
Popmodal
jquery plugin for showing tooltips, titles, modal dialogs and etc
Stars: ✭ 149 (+14.62%)
Mutual labels:  dialog, modal, popup
Customalertviewdialogue
Custom AlertView Dialogue is the world's most advanced alert view library. Custom AlertView Dialogue includes simple message popups, confirmation alerts, selector popups, action sheet bottom menus, and input/feedback contact forms.
Stars: ✭ 100 (-23.08%)
Mutual labels:  dialog, modal, popup
svelte-accessible-dialog
An accessible dialog component for Svelte apps
Stars: ✭ 24 (-81.54%)
Mutual labels:  modal, dialog, svelte
Nativepopup
Clone of Apple iOS App's feedback popup, and easily customizable.
Stars: ✭ 247 (+90%)
Mutual labels:  dialog, modal, popup
Sweetalert
A beautiful replacement for JavaScript's "alert"
Stars: ✭ 21,871 (+16723.85%)
Mutual labels:  dialog, modal, popup
React Native Simple Dialogs
⚛ Cross-platform React Native dialogs based on the Modal component
Stars: ✭ 218 (+67.69%)
Mutual labels:  dialog, modal, popup
React Native Alert Pro
The Pro Version of React Native Alert (Android & iOS)
Stars: ✭ 69 (-46.92%)
Mutual labels:  dialog, modal, popup
Pmalertcontroller
PMAlertController is a great and customizable alert that can substitute UIAlertController
Stars: ✭ 2,397 (+1743.85%)
Mutual labels:  dialog, modal, popup
eins-modal
Simple to use modal / alert / dialog / popup. Created with pure JS. No javascript knowledge required! Works on every browser and device! IE9
Stars: ✭ 30 (-76.92%)
Mutual labels:  modal, dialog, popup
react-redux-modal-flex
[DEPRECATED] Make easy a modal/popup with Redux
Stars: ✭ 14 (-89.23%)
Mutual labels:  modal, dialog, popup
Popupdialog
A simple, customizable popup dialog for iOS written in Swift. Replaces UIAlertController alert style.
Stars: ✭ 3,709 (+2753.08%)
Mutual labels:  dialog, modal, popup
React Native Push Notification Popup
A <NotificationPopup/> component for presenting your own push notification in react-native app
Stars: ✭ 111 (-14.62%)
Mutual labels:  dialog, modal, popup
Powermenu
🔥 The powerful and easiest way to implement modern material popup menu.
Stars: ✭ 822 (+532.31%)
Mutual labels:  dialog, popup

svelte-simple-modal

A simple, small, and content-agnostic modal for Svelte.


NPM Version Build Status File Size Code Style Prettier Demo

simple-modal

Live demo: https://svelte.dev/repl/033e824fad0a4e34907666e7196caec4?version=3.20.1

Works with: Svelte >=v3.4 (Tested until to v3.20)

Install

npm install --save svelte-simple-modal

Usage

Import the Modal component into your main Svelte component (e.g., App.svelte). The Modal is exposing two context functions open() and close() for opening and closing the modal. open() expects two arguments: a Svelte Component and optionally an object literal with the component's props.

<!-- App.svelte -->
<script>
  import Content from './Content.svelte';
  import Modal from 'svelte-simple-modal';
</script>

<Modal>
  <Content />
</Modal>


<!-- Content.svelte -->
<script>
  import { getContext } from 'svelte';
  import Surprise from './Surprise.svelte';

  const { open } = getContext('simple-modal');

  const showSurprise = () => {
    open(Surprise, { message: "It's a modal!" });
  };
</script>

<p><button on:click={showSurprise}>Show me a surprise!</button></p>


<!-- Surprise.svelte -->
<script>
  export let message;
</script>

<p>
  🎉 {message} 🍾
</p>

Configure your app bundler

IMPORTANT: In your main application's bundler you need to make sure that the svelte dependencies are resolved globally, meaning that the main application's version of svelte is used for bundling.

If you're using Rollup you can achieve this by setting the dedupe option of rollup-plugin-node-resolve as follows:

import resolve from 'rollup-plugin-node-resolve';

export default {
  plugins: [
    resolve({
      // Below is the important line!
      dedupe: ['svelte', 'svelte/transition', 'svelte/internal']
    }),
  ]
};

FOR SAPPER USERS: If you're using Sapper make sure you install svelte-simple-modal as a dev-dependency! If you're curious why please take a look at https://github.com/sveltejs/sapper-template#using-external-components

npm install -D svelte-simple-modal

Properties

The <Modal /> component accepts the following properties:

  • show: A Svelte component to show as the modal. See an alternative to the context API for details.
  • key: The context key that is used to expose open() and close(). Adjust to avoid clashes with other contexts. (Default: simple-modal)
  • setContext: You can normally ingore this property when you have configured your app bundler properly. If you want to bundle simple-modal with its own version of Svelte you have to pass setContext() from your main app to simple-modal using this parameter. (Default: setContext() of the associated svelte version.)
  • closeButton: If true a button for closing the modal is rendered. Note, you can also pass in a custom Svelte component as the close button to have full control over the styling. (Default: true)
  • closeOnEsc: If true the modal will close when pressing the escape key. (Default: true)
  • closeOnOuterClick: If true the modal will close when clicking outside the modal window. (Default: true)
  • transitionBg: Transition function for the background. (Default svelte:fade)
  • transitionBgProps: Properties of the transition function for the background. (Default {})
  • transitionWindow: Transition function for the window. (Default svelte:fade)
  • transitionWindowProps: Properties of the transition function for the window. (Default {})
  • styleBg: Style properties of the background. (Default {top: 0, left: 0})
  • styleWindowWrap: Style properties of the modal window wrapper element. (Default {})
  • styleWindow: Style properties of the modal window. (Default {})
  • styleContent: Style properties of the modal content. (Default {})
  • styleCloseButton: Style properties of the built-in close button. (Default {})

Events

The <Modal /> component dispatches the following events:

  • open: dispatched when the modal window starts to open.
  • opened: dispatched when the modal window opened.
  • close: dispatched when the modal window starts to close.
  • closed: dispatched when the modal window closed.

Alternatively, you can listen to those events via callbacks passed to open() and close().

Context API

You can access the context via getContext('simple-modal'). It exposes the following two methods:

# open(Component, props = {}, options = {}, callbacks = {})

Opens the modal with <Component {props}> rendered as the content. options can be used to adjust the modal behavior once for the modal that is about to be opened. The options allows to customize all parameters except key and setContext:

{
  closeButton: false,
  closeOnEsc: false,
  closeOnOuterClick: false,
  transitionBg: fade,
  transitionBgProps: {
    duration: 5000
  },
  transitionWindow: fly,
  transitionWindowProps: {
    y: 100,
    duration: 250
  },
  styleBg: { backgroundImage: 'http://example.com/my-background.jpg' },
  styleWindow: { fontSize: '20em' },
  styleContent: { color: 'yellow' },
  styleCloseButton: { width: '3rem', height: '3rem' }
}

Callbacks are triggered at the beginning and end of the opening and closing transition. The following callbacks are supported:

{
  onOpen: () => { /* modal window starts to open */ },
  onOpened: () => { /* modal window opened */ },
  onClose: () => { /* modal window starts to close */ },
  onClosed: () => { /* modal window closed */ },
}

Custom Close Button

This feature requires Svelte >=v3.19!

Unfortunately, it's not possible to adjust all styles of the built-in close button via the styleCloseButton option. If you need full control you can implement your own Svelte component and use that as the close button. To do so specify your component via the closeButton option as follows:

<!-- CloseButton.svelte -->
<script>
  // This property is used by Modal.svelte to pass down the close function
  export let onClose;
</script>

<style>
  /* Customize to your liking */
  button {
    position: absolute;
    top: -3rem;
    right: 0;
  }
</style>

<button on:click={onClose}>Custom Close Button</button>

<!-- Content.svelte -->
<script>
  import { getContext } from 'svelte';
  import Surprise from './Surprise.svelte';
  import CloseButton from './CloseButton.svelte';

  const { open } = getContext('simple-modal');

  const showSurprise = () => {
    open(Surprise, { message: "It's a modal!" }, { closeButton: CloseButton });
  };
</script>

<p><button on:click={showSurprise}>Show me a surprise!</button></p>

# close(callbacks = {})

Closes the modal. Similar to open(), this method supports adding callbacks for the closing transition:

{
  onClose: () => { /* modal window starts to close */ },
  onClosed: () => { /* modal window closed */ },
}

Context API Alternative

If you prefer stores over the context API, e.g., to open a modal from a component that is not a child of <Modal />, you can use the show property of <Modal /> as follows:

<!-- App.svelte -->
<script>
  import { writable } from 'svelte/store';
  import Content from './Content.svelte';
  import Popup from './Popup.svelte';
  export const modal = writable(null);

  function showModal() {
    modal.set(Popup)
  }
</script>

<Content />
<button on:click={showModal}>Show modal</button>


<!-- Content.svelte -->
<script>
  import Modal from 'svelte-simple-modal';
  import SomethingElse from './SomethingElse.svelte';
  import { modal } from './App.svelte';
</script>

<Modal show={$modal}>
  <SomethingElse />
</Modal>

Since it was not obvious to me how to pre-bind props to a Svelte component, I've added a helper function called bind that achieves this.

<script>
  import { bind } from 'svelte-simple-modal';
  import Popup from './Popup.svelte';
  import { modal } from './App.svelte';

  modal.set(bind(Popup, { name: 'custom name' }));
</script>

If you've worked with React/JSX then think of const c = bind(Component, props) as the equivalent of const c = <Component ...props />.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].