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

@fullkekw/fkw-popup

v1.1.1

Published

React Popup component written on Typescript. Compatible with Next & Vite!

Downloads

9

Readme

cover

React Popup component written on Typescript. Compatible with Next & Vite!

Features

  • Closing dialog on Escape or by click on Layout
  • Smooth scroll hiding without reseting content
  • Manipulating Dialog state out of component
  • Will return focus to the button that triggered opening
  • Possibility to prevent user change state of popup
  • Flexible settings
  • Fancy in-box styling
  • Implements WAI-ARIA Dialog pattern

Examples

Import components and styles

import { Layer, Dialog, Trigger } from "@fullkekw/fkw-popup";

import '@fullkekw/fkw-popup/css';

You can add inbox pretty styles by adding fkw-popup--fancy class to Layer component

Default implementation

Default implementation + state observing of Popup Component

const Component: React.FC = () => {
  const [id_1] = useState<string>('popup-1');
  const [isOpen, setIsOpen] = useState(true);

  // Observe popup state changes
  useEffect(() => console.log(isOpen), [isOpen])

  return (
    <div>
      <Layer className="fkw-popup--fancy" settings={{}}>
        <Dialog id={id_1} stateSetter={setIsOpen}>
          <p>DIALOG 1</p>

          <Trigger id={id_1}>
            <p>TRIGGER 1</p>
          </Trigger>
        </Dialog>
      </Layer>

      <Trigger id={id_1}>
        <p>TRIGGER 1</p>
      </Trigger>
    </div>
  );
};

Custom state handling

Implement custom state changing. Popup will be opened initially

const Component: React.FC = () => {
  const [id_1] = useState<string>('popup-1');
  const [isOpen, setIsOpen] = useState(true);

  // Observe popup state changes
  useEffect(() => console.log(isOpen), [isOpen])

  // Reverse popup state after 3 seconds
  useEffect(() => {
    setTimeout(() => {
      setIsOpen(prev => !prev)
    }, 3000)
  }, [])

  return (
    <div>
      <Layer className="fkw-popup--fancy" settings={{}}>
        <Dialog id={id_1} state={isOpen} stateSetter={setIsOpen}>
          <p>DIALOG 1</p>

          <Trigger id={id_1}>
            <p>TRIGGER 1</p>
          </Trigger>
        </Dialog>
      </Layer>

      <Trigger id={id_1}>
        <p>TRIGGER 1</p>
      </Trigger>
    </div>
  );
};

Prevent user from closing popup

Popup will be initially opened and user can not close it

const Component: React.FC = () => {
  const [id_1] = useState<string>('popup-1');
  const [isOpen, setIsOpen] = useState(true);

  // Observe popup state changes
  useEffect(() => console.log(isOpen), [isOpen])

  // Reverse popup state after 3 seconds
  useEffect(() => {
    setTimeout(() => {
      setIsOpen(prev => !prev)
    }, 3000)
  }, [])

  return (
    <div>
      <Layer className="fkw-popup--fancy" settings={{preventUserInteractions: true}}>
        <Dialog id={id_1} state={isOpen} stateSetter={setIsOpen}>
          <p>DIALOG 1</p>

          <Trigger id={id_1}>
            <p>TRIGGER 1</p>
          </Trigger>
        </Dialog>
      </Layer>

      <Trigger id={id_1}>
        <p>TRIGGER 1</p>
      </Trigger>
    </div>
  );
};

API

Open popup will hide any elements with tabindex. To prevent this, add fkw-prevent_hideIndexes class to element with tabindex. Trigger which open popup will have fkw-popup-trigger_triggeredBy class

export interface IPopupSettings {
  /** Hide body scroll when popup is active
   * @default true
   */
  hideScroll?: boolean

  /** Close popup on escape
   * @default true
   */
  exitOnEscape?: boolean

  /** Close popup by clicking on the layer
   * @default true
   */
  exitOnLayer?: boolean

  /** Prevent user from changing popup state
   * @default false
   */
  preventUserInteractions?: boolean
}



interface ILayerProps {
  children: React.ReactNode | React.ReactNode[]
  className?: string

  /** Popup settings */
  settings?: IPopupSettings
}

interface IDialogProps {
  children: React.ReactNode | React.ReactNode[]
  className?: string

  /** Popup ID */
  id: string

  /** Out state */
  state?: boolean

  /** Out state setter */
  stateSetter?: (state: boolean) => void
}

interface ITriggerProps {
  children: React.ReactNode | React.ReactNode[]
  className?: string
  onClick?: () => void

  /** Popup ID */
  id: string
}

/** Popup layer */
export const Layer: React.FC<ILayerProps>

/** Popup dialog */
export const Dialog: React.FC<IDialogProps>

/** Popup trigger. Already returning button element, nesting anither one can cause Hydration error in Next.JS */
export const Trigger: React.FC<ITriggerProps>

Handlers

/** Hide scrollbar with saving scroll width
 * @param extendElement HTML element to extend. Default - document.body
 */
export function toggleScroll(hide: boolean, extendElement?: HTMLElement)

/** Hide tab indexes 
 * Will store them in data-fkw-prevTabIndex
 * @param except Class that this function will pass. Will except fkw-prevent_hideIndexes by default
*/
export function hideTabIndexes(except?: string)

/** Return tab indexes from data-fkw-prevTabIndex */
export function showTabIndexes()

/** Force element focus */
export function forceFocus(el: HTMLElement)

Get it now!

npm install @fullkekw/fkw-popup

Licensed under MIT fullkekw © 2023 - 2024