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

tiptap-slash-react

v1.1.2

Published

A Tiptap extension for slash commands in react

Downloads

601

Readme

tiptap-slash-react

A slash command extension for the Tiptap editor in React applications.

Features

  • Easy integration with Tiptap editor
  • Customizable command items
  • Typescript support

To use it, type / and after selecting a command with the up and down keys, type / again to execute it.

Installation

Install the package using npm:

npm install tiptap-slash-react

Usage

Basic Setup

  1. Import the necessary components:

For Typescript and Javascript

import { SlashSuggestion, filterCommandItems } from "tiptap-slash-react";
  1. Add the extension to your Tiptap editor:

For Typescript

const editor = useEditor({
  extensions: [
    SlashSuggestion.configure({
      suggestion: {
        items: ({ query }: { query: string }) => filterCommandItems(query),
      },
    }),
  ],
});

For Javascript

const editor = useEditor({
  extensions: [
    SlashSuggestion.configure({
      suggestion: {
        items: ({ query }) => filterCommandItems(query),
      },
    }),
  ],
});

Customizing Command Items

You can create custom command items to tailor the slash commands to your needs:

For Javascript

const createHeadingCommand =
  (level) =>
  ({ editor, range }) => {
    editor
      .chain()
      .focus()
      .deleteRange(range)
      .setNode("heading", { level })
      .run();
  };

const customCommandItems = [
  { title: "Heading 1", icon: Heading1, command: createHeadingCommand(1) },
  { title: "Heading 2", icon: Heading2, command: createHeadingCommand(2) },
  { title: "Heading 3", icon: Heading3, command: createHeadingCommand(3) },
];

const editor = useEditor({
  extensions: [
    SlashSuggestion.configure({
      commandItems: customCommandItems,
      suggestion: {
        items: ({ query }) => filterCommandItems(query, customCommandItems),
      },
    }),
  ],
});

For Typescript:

import {
  SlashSuggestion,
  filterCommandItems,
  CustomCommandItem,
} from "tiptap-slash-react";
const createHeadingCommand =
  (level: number) =>
  ({ editor, range }: { editor: Editor; range: Range }) => {
    editor
      .chain()
      .focus()
      .deleteRange(range)
      .setNode("heading", { level })
      .run();
  };

const customCommandItems: CustomCommandItem[] = [
  { title: "Heading 1", icon: Heading1, command: createHeadingCommand(1) },
  { title: "Heading 2", icon: Heading2, command: createHeadingCommand(2) },
  { title: "Heading 3", icon: Heading3, command: createHeadingCommand(3) },
];

const editor = useEditor({
  extensions: [
    SlashSuggestion.configure({
      commandItems: customCommandItems,
      suggestion: {
        items: ({ query }: { query: string }) =>
          filterCommandItems(query, customCommandItems),
      },
    }),
  ],
});

Styling

To style the slash command menu, add the following CSS to your project and customize as needed:

.slash-menu {
  position: relative;
  border-radius: 0.5rem;
  background: white;
  color: #333;
  overflow: hidden;
  font-size: 0.9rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 1px 3px rgba(0, 0, 0, 0.08);
  max-height: 300px;
  overflow-y: auto;
  padding: 0.5rem;
}

.slash-menu__item {
  display: flex;
  align-items: center;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  padding: 0.5rem;
  border-radius: 0.25rem;
  transition: background-color 0.2s ease;
}

.slash-menu__item-icon {
  width: 20px;
  height: 20px;
  margin-right: 12px;
  color: #6b7280;
}

.slash-menu__item-title {
  font-size: 14px;
  font-weight: 500;
}

.slash-menu__item--disabled {
  opacity: 0.5;
  pointer-events: none;
}

.slash-menu__item--selected,
.slash-menu__item:hover {
  color: #4f46e5;
  background: rgba(79, 70, 229, 0.1);
}

License

This project is licensed under the MIT License - see the LICENSE file for details.