Spotlight
Installation
Package depends on @mantine/core
and @mantine/hooks
.
Install with npm:
Install with yarn:
Usage
Spotlight component let you build a popup search interface which can be triggered with keyboard
shortcut or programmatically from anywhere inside your application. To get started, wrap your application
with SpotlightProvider
component:
Note that if you are using MantineProvider, SpotlightProvider
must be used as its child:
Keyboard shortcuts
SpotlightProvider
uses use-hotkeys hook to add keyboard shortcuts,
the default shortcut to trigger popup is mod + K
, it means that it will be shown
when users press ⌘ + K
on MacOS and Ctrl + K
on any other os.
You can setup multiple shortcuts, for example, Mantine documentation uses the following setup:
It means that user will be able to open documentation search with the following shortcuts:
⌘ + K
/Ctrl + K
⌘ + P
/Ctrl + P
/
– single keys are also supported
Note that provided shortcuts will prevent the default behavior, for example, mod + P
will
disable "Print page" native browser function, choose those shortcuts that will not interfere
with desired default browser behavior.
Keyboard shortcuts will not work if:
- focus is not on current page
input
,textarea
orselect
elements are focused
To disabled keyboard shortcuts set shortcut={null}
:
use-spotlight hook
use-spotlight
hook lets you control spotlight from anywhere in your application.
For example, it can be used to open spotlight with button click:
use-spotlight
returns an object with the following properties:
Event based functions
@mantine/spotlight
exports several functions which can be used to perform certain actions
from any part of your application:
Spotlight actions
actions
is the only required prop of SpotlightProvider
. Action shape:
You can import SpotlightAction
type from @mantine/spotlight
package:
Actions filtering
When user searches spotlight actions are filtered based on the following action properties:
title
–string
description
–string
keywords
–string | string[]
You can change filtering logic by setting filter
prop on SpotlightProvider
.
The following example filters actions only by title:
Actions limit
If you have a large list of actions, most of them will not be get to the initial list (when user have not enter any text yet).
You can control how many actions are displayed at a time with limit
prop:
Register additional actions
You can register any amount of additional actions with registerActions
function on use-spotlight
hook.
To remove actions from the list use removeActions
function:
Group actions
Custom action component
You can provide custom component to render actions, this feature can be used to customize how actions are displayed:
Custom actions wrapper component
With custom actions wrapper component you can customize how actions list is rendered, for example, you can add a footer:
Close spotlight on action trigger
By default, spotlight will be closed with any action is triggered with mouse click or Enter
key.
To change this behavior set closeOnActionTrigger={false}
prop on SpotlightProvider
:
Highlight query
Default action component supports highlighting search query with Highlight component.
To enable this option set highlightQuery
on SpotlightProvider
:
Change transitions
Component presence is animated with Transition component, it supports all premade and custom transitions. To change transition set the following properties:
transition
– premade transition name or custom transition objecttransitionDuration
– transition duration in ms
To disable transitions set transitionDuration={0}
: