import React, { useContext, useEffect } from "react"; import { Story, Meta } from "@storybook/react"; import { Action, CommandPalette, CommandPaletteProps } from "../CommandPalette"; import CommandPaletteContext from "../CommandPalette/CommandPaletteContext"; import "./page.css"; import { LoremIpsum } from "lorem-ipsum"; import { useHotkeys } from "react-hotkeys-hook"; export default { title: "CommandPalette/Basics", component: CommandPalette, // argTypes: { // backgroundColor: { control: "color" }, // }, } as Meta; const lorem = new LoremIpsum({ sentencesPerParagraph: { max: 8, min: 4, }, wordsPerSentence: { max: 16, min: 4, }, }); const testActions: Action[] = Array.from({ length: 50 }, (_, i) => ({ id: i, title: lorem.generateSentences(1), onSelect: () => { alert("You selected action #" + i); }, })); const Template: Story = (args) => ( Render your children within the {""} to access the context. ); const ToggleCommandPaletteButton = () => { const { toggle } = useContext(CommandPaletteContext); return ; }; const ComponentWhichRegistersActions = () => { const { addAction, removeAction } = useContext(CommandPaletteContext); useEffect(() => { testActions.forEach((action) => addAction(action)); return () => { testActions.forEach((action) => removeAction(action)); }; }, [addAction, removeAction]); return null; }; export const Default = Template.bind({}); Default.args = {}; export const CustomPlaceholder = Template.bind({}); CustomPlaceholder.args = { InputProps: { placeholder: "Imagine if you couldn't search through all this... 😌", }, }; const ComponentWhichTogglesCommandPalette = () => { const { toggle } = useContext(CommandPaletteContext); useHotkeys( "shift+p", (e) => { // Prevent default is here because CTRL + P usually starts the print dialog e.preventDefault(); toggle(); }, { enableOnTags: ["INPUT"] } ); return null; }; const CustomKeybindingTemplate: Story = (args) => (

Press shift + p to toggle the command palette.

); export const CustomKeybinding = CustomKeybindingTemplate.bind({}); CustomKeybinding.args = {};