Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@mantine/spotlight] Add closeOnTrigger prop to SpotlightAction (#2040) #2050

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/src/docs/others/spotlight.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ To change this behavior set `closeOnActionTrigger={false}` prop on `SpotlightPro

<Demo data={SpotlightDemos.closeOnTrigger} />

## Close spotlight on specific action trigger

Other than with the global `closeOnActionTrigger` property, the `closeOnTrigger` property can be defined
for specific actions. The action will then ignore the closeOnActionTrigger property and use its own definition.

<Demo data={SpotlightDemos.actionsCloseOnTrigger} />

## Highlight query

Default action component supports highlighting search query with [Highlight](/core/highlight/) component.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { SpotlightAction } from '@mantine/spotlight';
import React from 'react';
import { Wrapper } from './_wrapper';

const code = `
import { SpotlightProvider, SpotlightAction } from '@mantine/spotlight';

const onTrigger = () => {};

const actions: SpotlightAction[] = [
{ title: 'Will stay open', onTrigger, closeOnTrigger: false },
{ title: 'Will close', onTrigger, closeOnTrigger: true },
];

function Demo() {
return (
<SpotlightProvider
actions={actions}
searchPlaceholder="Search..."
shortcut="mod + shift + 5"
>
<App />
</SpotlightProvider>
);
}
`;

const onTrigger = () => {};

const actions: SpotlightAction[] = [
{ title: 'Will stay open', onTrigger, closeOnTrigger: false },
{ title: 'Will close', onTrigger, closeOnTrigger: true },
];

function Demo() {
return <Wrapper actions={actions} searchPlaceholder="Search..." shortcut="mod + shift + 5" />;
}

export const actionsCloseOnTrigger: MantineDemo = {
type: 'demo',
component: Demo,
code,
};
1 change: 1 addition & 0 deletions src/mantine-demos/src/demos/spotlight/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { transitionNone } from './Spotlight.demo.transitionNone';
export { customTransition } from './Spotlight.demo.customTransition';
export { large } from './Spotlight.demo.large';
export { closeOnTrigger } from './Spotlight.demo.closeOnTrigger';
export { actionsCloseOnTrigger } from './Spotlight.demo.actionsCloseOnTrigger';
export { register } from './Spotlight.demo.register';
export { groups } from './Spotlight.demo.groups';
export { actionComponent } from './Spotlight.demo.actionComponent';
Expand Down
17 changes: 17 additions & 0 deletions src/mantine-spotlight/src/Spotlight.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,21 @@ storiesOf('Spotlight', module)
<Wrapper {...defaultProps}>
<RegisterInEffect />
</Wrapper>
))
.add('Actions with closeOnTrigger', () => (
<Wrapper
{...defaultProps}
actions={[
{
title: 'Should stay open',
onTrigger: () => console.log('Should stay open'),
closeOnTrigger: false,
},
{
title: 'Should close',
onTrigger: () => console.log('Should close'),
closeOnTrigger: true,
},
]}
/>
));
2 changes: 1 addition & 1 deletion src/mantine-spotlight/src/Spotlight/Spotlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export function Spotlight({
onActionHover={setHovered}
onActionTrigger={(action) => {
action.onTrigger(action);
closeOnActionTrigger && handleClose();
(action.closeOnTrigger ?? closeOnActionTrigger) && handleClose();
}}
styles={styles}
classNames={classNames}
Expand Down
3 changes: 3 additions & 0 deletions src/mantine-spotlight/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export interface SpotlightAction {
/** Function that is called when action is triggered */
onTrigger(action: SpotlightAction): void;

/** If the spotlight is closed after clicking on this action */
closeOnTrigger?: boolean;

/** Any other properties that will be consumed by SpotlightProvider */
[key: string]: any;
}