npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@solisoma/svelte-modal

v1.1.2

Published

The **@solisoma/svelte-modal** package provides a customizable and easy-to-use modal component for Svelte applications. With this package, you can quickly integrate modals into your Svelte projects with minimal configuration.

Downloads

8

Readme

@solisoma/svelte-modal

The @solisoma/svelte-modal package provides a customizable and easy-to-use modal component for Svelte applications. With this package, you can quickly integrate modals into your Svelte projects with minimal configuration.

Installation

You can install the @solisoma/svelte-modal package via npm:

npm install @solisoma/svelte-modal

Usage

To use the modal component in your Svelte application, import it into your component and include it in your template.

<script>
  import Modal from '@solisoma/svelte-modal';
</script>

<Modal>
  <!-- Your modal content goes here -->
</Modal>

Keeping it at the bottom of your html code is recommended;

<script>
  import Modal from '@solisoma/svelte-modal';
</script>

<main>
    <!-- Your HTML tags goes here-->
</main>

<Modal>
  <!-- Your modal content goes here -->
</Modal>

Props

The @solisoma/svelte-modal supports the following props:

  • visible: Boolean value indicating whether the modal is visible.
  • useHtml: Boolean value, indicating that we are using the @html for rendering. That means Modal should expect a string.
  • setHtml: String value, holding the html to be rendered.
  • onClose: Callback function invoked when the modal is closed. It passes state as first argument to the function, if initialState was given.
  • initialState: Object value, holding the initial state of the form in the modal.
  • classNames: String value use for styling the modal.
  • component: Svelte component passed as value to be rendered inside the modal.

@solisoma/svelte-modal act like a parent component expecting a child. The child is the actual content to be rendered inside the Modal.

There are three ways to pass a child to the Modal:

  • Using string html
  • Passing a Svelte component to the component prop
  • Passing a Svelte component as a direct child

Let break each method down:

Using string html

Here @solisoma/svelte-modal expects a html written in string format. It uses the @html tag to render the html. Let's write a code to explain better.

<script>
  import Modal from '@solisoma/svelte-modal';
</script>

<main>
    <!-- Your HTML tags goes here-->
</main>

<Modal visible useHtml setHtml="<div>HelloWorld!</div>" />

It's important to use useHtml and setHtml with caution, as it can expose your application to security vulnerabilities if used improperly, such as by injecting untrusted or unsanitized user input. Always validate and sanitize user-generated content before using it with setHtml to prevent cross-site scripting (XSS) attacks.

Passing a Svelte component to the component prop

To implement this feature, you'll need to create another Svelte component responsible for rendering the interface of the Modal. Keep in mind that @solisoma/svelte-modal passes certain props to its child, whether passed through the component prop or as a direct child. These props include:

  • closeModal: A function prop that enables the developer to close the Modal based on events like on:click. It doesn't require any parameters.

  • modalState: This prop holds the current value of the state passed to the Modal.

  • setModalState: Used for setState, this prop updates the state of the modalState.

Here's an example of the child component:

// Component.svelte

<script>
  export let closeModal;
  export let modalStates;
  export let setModalState;
</script>

<div>
  <p>Hello</p>
  <button on:click={closeModal}>Close Modal</button>
</div>

And here's how you would use it with the Modal component:

<script>
  import Component from "./Component.svelte";
  import Modal from '@solisoma/svelte-modal';
</script>

<main>
  <!-- Your HTML tags go here -->
</main>

<Modal
  visible
  onClose={() => console.log('Modal closed')}
  initialState={{ name: "solisoma", desc: "modal" }}
  component={Component}
/>

In the provided example, the child component is passed using the component prop.

Passing a Svelte component as a direct child

In this approach, similar to the previous method, we include the child component directly as a child of the Modal component.

Example:

// Component.svelte

<script>
  export let closeModal;
  export let modalStates;
  export let setModalState;
</script>

<div>
  <p>Hello</p>
  <button on:click={closeModal}>Close Modal</button>
</div>
<script>
  import Component from "./Component.svelte";
  import Modal from '@solisoma/svelte-modal';
</script>

<main>
  <!-- Your HTML tags go here -->
</main>

<Modal
  visible
  onClose={() => console.log('Modal closed')}
  initialState={{ name: "solisoma", desc: "modal" }}
>
  <Component />
</Modal>

This method allows you to pass additional props to the child component if needed, as shown below:

<script>
  import Component from "./Component.svelte";
  import Modal from '@solisoma/svelte-modal';
</script>

<main>
  <!-- Your HTML tags go here -->
</main>

<Modal
  visible
  onClose={() => console.log('Modal closed')}
  initialState={{ name: "solisoma", desc: "modal" }}
>
  <Component borderColor="red" />
</Modal>

Styling

The Modal component comes with minimal default styling to ensure flexibility and ease of customization. You can apply your own styles by passing them to the classNames prop. The classNames prop accepts only class names. It's important to note that any styles provided to the modal will override the default styling.

Here's an example of applying custom styles using TailwindCSS:

<Modal
  visible
  onClose={() => console.log('Modal closed')}
  initialState={{ name: "solisoma", desc: "modal" }}
  classNames="border-2 border-red-300"
>
  <Component borderColor="red" />
</Modal>

While TailwindCSS is used in the example, you can utilize any CSS framework or custom styles to achieve the desired look and feel for your modal component.