|
| 1 | +## Installation |
| 2 | + |
| 3 | +```sh |
| 4 | +npm install --save-dev react-ezql |
| 5 | +``` |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +**Minimal usage** |
| 10 | + |
| 11 | +```tsx |
| 12 | +import EzqlPrompt from 'react-ezql' |
| 13 | + |
| 14 | +<EzqlPrompt |
| 15 | + setShouldDisplayEzql={setShouldDisplayEzql} |
| 16 | + suggestions={['How many books sold last week', 'How many new users signed up today']} |
| 17 | + didSubmitWithValue={(value) => { |
| 18 | + console.log(`query: ${value}`) |
| 19 | + setShouldDisplayEzql(false) |
| 20 | + }} |
| 21 | + onResults={(json) => console.dir(json)} |
| 22 | + className="optional-for-styling-convenience" |
| 23 | +/> |
| 24 | +``` |
| 25 | + |
| 26 | +**Example App.tsx** |
| 27 | +Here lies a demonstration conditionally showing/hiding the prompt |
| 28 | + |
| 29 | +```tsx |
| 30 | +import './App.css' |
| 31 | +import EzqlPrompt from 'react-ezql' |
| 32 | +import { useEffect, useState } from 'react' |
| 33 | + |
| 34 | +const TRIGGERS = ['KeyK', 'Slash'] |
| 35 | + |
| 36 | +function App() { |
| 37 | + const [shouldDisplayEzql, setShouldDisplayEzql] = useState(false) |
| 38 | + const suggestions = ['How many books sold last week', 'How many new users signed up today'] |
| 39 | + |
| 40 | + // listen for the trigger to display the modal |
| 41 | + useEffect(() => { |
| 42 | + function ezqlTriggerListener(ev: KeyboardEvent) { |
| 43 | + if (TRIGGERS.includes(ev.code) && ev.getModifierState('Meta')) { |
| 44 | + setShouldDisplayEzql(true) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // BEGIN listening for the trigger(s) |
| 49 | + document.addEventListener('keydown', ezqlTriggerListener) |
| 50 | + |
| 51 | + return () => { |
| 52 | + // STOP listening for the trigger(s) |
| 53 | + document.removeEventListener('keydown', ezqlTriggerListener) |
| 54 | + } |
| 55 | + }, []) |
| 56 | + |
| 57 | + return ( |
| 58 | + <> |
| 59 | + {shouldDisplayEzql && ( |
| 60 | + <EzqlPrompt |
| 61 | + setShouldDisplayEzql={setShouldDisplayEzql} |
| 62 | + suggestions={suggestions} |
| 63 | + didSubmitWithValue={(value) => { |
| 64 | + console.log(`query: ${value}`) |
| 65 | + setShouldDisplayEzql(false) |
| 66 | + }} |
| 67 | + onResults={(json) => console.dir(json)} |
| 68 | + className="optional-for-styling-convenience" |
| 69 | + /> |
| 70 | + )} |
| 71 | + </> |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +export default App |
| 76 | +``` |
| 77 | + |
| 78 | +## Styling |
| 79 | + |
| 80 | +<!-- TODO? Perhaps we make it possible to utilize CSS variables (when defined) --> |
| 81 | + |
| 82 | +Each HTML Element has been an assigned a meaningful classname that you can style in your own CSS |
| 83 | + |
| 84 | +- Use the web inspector to reveal which class names are assigned to which elements |
| 85 | +- You may optionally pass `className` to the component, i.e. `<EzqlPrompt className="arbitrary-value">`. This allows more precise scoping of CSS: |
| 86 | + |
| 87 | +```css |
| 88 | +.arbitrary-value.ezql-prompt-modal { |
| 89 | + background-color: black; |
| 90 | +} |
| 91 | +, |
| 92 | +.arbitrary-value .input-container { |
| 93 | + color: white; |
| 94 | +} |
| 95 | +``` |
0 commit comments