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

@ws-serenity/react-modals

v3.0.0

Published

React modal dialogs

Downloads

23

Readme

Простейшие компоненты модального окна

Repository

Суть в отсутствии каких-либо "лишних стилей", что позволяет без проблем настраивать модальное окно под любой проект

Components

  1. ModalBase
  2. ModalWindow
  3. ModalDialog
  4. useModalSpawner

ModalBase

Базовое модальное окно. Его можно закрыть только нажатием вне его области

export type ModalBaseProps = {
    // вложенные элементы
    children: ReactNode;
    // слой с модалкой
    layoutClassName?: string;
    // контейнер модалки
    containerClassName?: string;
    // закрывать ли модалку по клику вне её
    closeOnBackClick?: boolean;

    // функция закрытия модалки
    onClose(): void;
}
Пример использования
<ModalBase
    closeOnBackClick={true}
    layoutClassName={'modal-base-layout'}
    containerClassName={'modal-base-container'}
>
    <p>Inside text</p>
</ModalBase>

ModalWindow

Шаблон с тайтлом, футером и иконкой закрытия модалки

export interface ModalWindowProps extends ModalBaseProps {
    // заголовок
    title: string | JSX.Element;
    // иконка закрытия
    closeIcon?: JSX.Element;
    // футер
    footer?: JSX.Element;
}
Пример использования
<ModalWindow
    title={'Here I am'}
    layoutClassName={'modal-window-layout'}
    containerClassName={'modal-window-container'}
    footer={<p>Footer</p>}
    onClose={() => alert('close'))}
>
    <p>Inside text</p>
</ModalWindow>

ModalDialog

Шаблон, в который можно передать несколько кнопок

export interface ModalDialogProps<TProps> extends ModalBaseProps {
    // компонент кнопок
    button: (props: TProps) => JSX.Element;
    // пропсы для каждой кнопки
    buttons: TProps[];
}
Пример использования
interface ButtonProps {
    label: string;
    className?: string;

    onClick(): void;
}

const Button = (props: ButtonProps) => {
    const { label, className, onClick } = props;

    return (
        <button
            className={className}
            onClick={onClick}
        >
            {label}
        </button>
    );
};

const SomeComponent = () => {
    const buttons = [
        {
            label: 'Send',
            onClick: () => alert('send'),
        },
        {
            label: 'Cancel',
            onClick: () => alert('cancel'),
        },
    ];

    return (
        <ModalDialog
            layoutClassName={'modal-dialog-layout'}
            containerClassName={'modal-dialog-container'}
            button={Button}
            buttons={buttons}
            onClose={() => alert('close')}
        >
            <p>Inside text</p>
        </ModalDialog>
    );
};

UseModalSpawner

Хук для работы с модальным окном. Предоставляет функции по открытию и закрытию модалки В месте расположения компонента ModalSpawner хук useModalSpawner спавнит определенную модалку, переданную в open

// типы функций хука useModalSpawner
type ModalControl<E> = {
    open(props: E): void;
    close(): void;
}
Пример использования
const AppWrapper = () => {
    return (
        <ModalSpawnerProvider>
            <ModalSpawner/>
            <InsideComponent/>
        </ModalSpawnerProvider>
    );
};

const InsideComponent = () => {
    const { open, close } = useModal(Modal);

    return (
        <button
            className={'show-modal-button'}
            onClick={() => open({
                title: 'Modal Spawner',
                onClose: close,
            })}
        >
            Show modal
        </button>
    );
};

function Modal({ title, onClose }: { title: string, onClose: () => void; }) {
    return (
        <ModalWindow
            title={title}
            onClose={onClose}
        >
            <p>Children</p>
        </ModalWindow>
    );
};