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

svelte-hypercommands

v0.1.5

Published

A Headless Hyper Palette for your Svelte app

Downloads

10

Readme

Hypercommands

svelte-hypercommands is a dependancy free Headless Palette for creating command/navigation/search palettes for your Svelte and SvelteKit app.

The main objetive is enabling complete customization and extensibility of a base Command Palette component while providing a default implementation that can be used out of the box.

Its important to remark that the headless logic comes from Melt-UI and the keybindings logic is a modified version of Tinykeys. Each of these libraries deserve all the credit for their work.

[!IMPORTANT] This library is still in early development and the API is subject to breaking changes.

Features

  • Actionable/Navigable/Searchable types
  • Default implementation (commmands/pages modes)
  • Dependency free
  • Dynamic registration
  • Fully typed
  • Headless logic
  • Keybindings support for actionables
  • SSR support

Installation

npm install svelte-hypercommands
# or
pnpm add svelte-hypercommands
# or
yarn add svelte-hypercommands

Getting started

The library is composed of two main parts: the default implementation and the headless logic.

The default implementation is a standalone component that can be used out of the box, while the headless logic can be used to create your own implementation for different palettes.

Default implementation

The default implementation mimics the behavior of the VSCode Command Palette, with two modes: commands and pages.

The commands mode is used to display a list of actionable items, while the pages mode is used to display a list of routes to navigate to.

If you want the palette to be globally available, use it in your global layout or in a layout that is used in all your pages.

<script>
  import { navigablesFromRoutes } from 'svelte-hypercommands';
  import HyperPalette, { defineCommand } from 'svelte-hypercommands/HyperPalette.svelte';

  const globalCommands = defineCommand(
    {
      name: 'Copy Current URL',
      description: 'Copy the current URL to your clipboard',
      shortcut: ['$mod+Shift+C'],
      onAction: () => {
        navigator.clipboard.writeText(window.location.href);
        alert('Copied URL to clipboard');
      }
    },
    {
      name: 'Reload Window',
      description: 'Reload the current window',
      shortcut: ['$mod+Shift+R'],
      onAction: () => {
        window.location.reload();
      }
    }
  );

  const globalPages = navigablesFromRoutes();
</script>

<HyperPalette commands={globalCommands} pages={globalPages} />

<slot />

<!-- or in svelte 5 -->

{@render children()}

For adding dynamic commands/pages you can use the helpers exported by the component:

<script>
  import { defineCommand, definePage, helpers } from 'svelte-hypercommands/HyperPalette.svelte';
  import { onMount } from 'svelte';

  onMount(() => {
    const cleanupCommands = helpers.registerCommand(
      defineCommand(
        {
          name: 'Say hello world',
          description: 'Alerts hello world',
          onAction: () => {
            alert('Hello World');
          },
        }
      )
    );

    const cleanupPages = helpers.registerPage(
      definePage(
        {
          name: 'SvelteKit Docs',
          description: 'Link to the Official SvelteKit documentation',
          url: 'https://kit.svelte.dev/docs/introduction',
        }
      )
    );

    return () => {
      cleanupCommands();
      cleanupPages();
    };
  });
</script>

<main>
  <h1>Home</h1>
</main>

Headless usage

The library exposes a createPalette function that can be used to create all the logic and state handling for a custom palette.

For an implementation reference, you can check the HyperPalette.svelte.

When creating your own implementation the createPalette receives an options object to configure the behavior of the palette and the modes that it will handle of the shape:

type PaletteOptions = {
    /**
     * Whether to close the palette when the user clicks outside of it.
     * 
     * @default true
     */
    closeOnClickOutside?: boolean;
    /**
     * Whether to close the palette when the user presses the escape key.
     * 
     * @default true
     */
    closeOnEscape?: boolean;
    /**
     * Debounce time for processing the search input in milliseconds.
     * 
     * A value greater than 0 will debounce the input. Otherwise, the input will be processed immediately.
     * 
     * @default 150
     */
    debounce?: number;
    /**
     * Default values for initializing the palette.
     */
    defaults?: PaletteDefaultsOptions<string>;
    /**
     * The configuration for the different modes of the palette.
     * 
     * Each key is the name of the mode and the value is the configuration for that mode.
     */
    modes: PaletteModesOptions;
    /**
     * A `Writable` store to control the open state of the palette from outside.
     */
    open?: Writable<boolean>;
    /**
     * A `Writable` store to control the placeholder of the search input from outside.
     */
    placeholder?: Writable<string | undefined>;
    /**
     * The target element to append the palette to.
     * 
     * - `false` portal is disabled.
     * - `string` a css selector for the portal target.
     * - `HTMLElement` the portal target.
     * 
     * @default false
     */
    portal?: HTMLElement | string | false;
    /**
     * Whether to reset the palette state when it is opened.
     * 
     * When set to `true` takes precedence over the `closeAction` of the modes.
     * 
     * @default false
     */
    resetOnOpen?: boolean;
};

The function returns an object of the shape:

type CreatePaletteReturn = {
    elements: {
        palette: BuilderPalette,
        panel: BuilderPanel,
        form: BuilderForm,
        label: BuilderLabel,
        input: BuilderInput,
        item: BuilderItem,
    },
    helpers: {
        registerItem: (mode: Mode, item: OneOrMany<AnyHyperItem>, override?: boolean, silent?: boolean) => Cleanup;
        unregisterItem: (mode: Mode, item: OneOrMany<ItemMatcher<AnyHyperItem>>) => void;
        search: (pattern: string) => void;
        openPalette: (mode?: Modes) => void;
        closePalette: () => void;
        togglePalette: () => void;
        registerPaletteShortcuts: () => void;
        unregisterPaletteShortcuts: () => void;
    };
    states: {
        open: Writable<boolean>;
        error: Writable<PaletteError | undefined>;
        mode: Writable<Modes>;
        modes: {
            [Mode in Modes] : {
                items: Writable<AnyHyperItem[]>;
                results: Writable<AnyHyperItem[]>;
                history: Writable<HyperItemId[]>;
                current: Writable<AnyHyperItem | undefined>;
            };
            placeholder: Writable<string | undefined>;
            portal: Writable<HTMLElement | string | false>;
            searchInput: Writable<string>;
        };
    };
};

The elements object contains the builders for the different elements needed to create the palette markup.

The states object contains the stores that hold the internal state of the palette.

The helpers object contains the functions to interact with the palette and its items.

Caution

Altering the internal state of the palette directly with the stores of the states object isn't guaranteed to work as expected. The helpers object should be used to interact with the palette state and its items.

Acknowledgements

Contributing

Any kind of contribution is welcome.

If you have any suggestions, feedback or issues, please open an issue or submit a pull request with your fix or feature.

License

MIT