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

fixed not being able to click out of command palette #641

Merged
merged 8 commits into from
Feb 10, 2025
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
5 changes: 5 additions & 0 deletions .changeset/tricky-onions-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/graph-editor": patch
---

Fixed not being able to click out of Command Palette
2 changes: 1 addition & 1 deletion .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ jobs:
run: yarn run test


- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: always()
with:
name: cypress-artifacts
Expand Down
6 changes: 6 additions & 0 deletions packages/graph-editor/src/components/commandPalette/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useSelectAddedNodes } from '@/hooks/useSelectAddedNodes.js';
import React from 'react';
import Search from '@tokens-studio/icons/Search.js';
import styles from './index.module.css';
import useClickOutside from '@/hooks/useClickOutside.js';

export interface ICommandMenu {
items: DropPanelStore;
Expand Down Expand Up @@ -91,6 +92,10 @@ const CommandMenu = ({ items, handleSelectNewNodeType }: ICommandMenu) => {
const reactflow = useReactFlow();
const selectAddedNodes = useSelectAddedNodes();

const dialogRef = useClickOutside<HTMLDivElement>(() => {
dispatch.ui.setShowNodesCmdPalette(!showNodesCmdPalette);
}, showNodesCmdPalette);

const handleSelectItem = (item) => {
const newNode = handleSelectNewNodeType({
position: reactflow.screenToFlowPosition(cursorPositionRef.current),
Expand Down Expand Up @@ -142,6 +147,7 @@ const CommandMenu = ({ items, handleSelectNewNodeType }: ICommandMenu) => {

return (
<Command.Dialog
ref={dialogRef}
open={showNodesCmdPalette}
onOpenChange={() =>
dispatch.ui.setShowNodesCmdPalette(!showNodesCmdPalette)
Expand Down
34 changes: 34 additions & 0 deletions packages/graph-editor/src/hooks/useClickOutside.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useEffect, useRef } from 'react';

export default <T extends HTMLElement>(
callback: () => void,
active = false,
) => {
const ref = useRef<T>(null);

useEffect(() => {
const handleClick = (event: MouseEvent) => {
const target = event.target as HTMLElement;

if (
[...target.classList.values()].find((itm) =>
itm.includes('react-colorful'),
)
) {
return;
}

callback();
};

if (active) {
document.addEventListener('click', handleClick, true);
}

return () => {
document.removeEventListener('click', handleClick, true);
};
}, [callback, ref, active]);

return ref;
};
Loading