Skip to content

✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.

refactor: streamline SearchInput component and enhance Toolbar layout with styled components

AykutSaracd49fe9895f16
diff --git a/apps/www/src/features/editor/Toolbar/SearchInput.tsx b/apps/www/src/features/editor/Toolbar/SearchInput.tsx
index 7b131097..8b12271f 100644
--- a/apps/www/src/features/editor/Toolbar/SearchInput.tsx
+++ b/apps/www/src/features/editor/Toolbar/SearchInput.tsx
@@ -1,6 +1,6 @@
import React from "react";
-import { ActionIcon, Text, TextInput, Tooltip } from "@mantine/core";
-import { getHotkeyHandler, useOs } from "@mantine/hooks";
+import { ActionIcon, TextInput, Tooltip } from "@mantine/core";
+import { getHotkeyHandler } from "@mantine/hooks";
import styled from "styled-components";
import { AiOutlineSearch } from "react-icons/ai";
import { LuChevronDown, LuChevronUp, LuX } from "react-icons/lu";
@@ -10,36 +10,10 @@ interface SearchInputProps {
onClose?: () => void;
}
-const Wrapper = styled.div`
- display: flex;
- flex-direction: column;
- gap: 6px;
- min-width: 320px;
-`;
-
const Row = styled.div`
display: flex;
align-items: center;
gap: 4px;
- padding: 2px 4px 2px 8px;
- border-radius: 8px;
- background: ${({ theme }) =>
- theme.BACKGROUND_SECONDARY === "#f2f3f5"
- ? "rgba(15, 23, 42, 0.04)"
- : "rgba(255, 255, 255, 0.05)"};
- border: 1px solid
- ${({ theme }) =>
- theme.BACKGROUND_SECONDARY === "#f2f3f5"
- ? "rgba(15, 23, 42, 0.08)"
- : "rgba(255, 255, 255, 0.08)"};
- transition: border-color 120ms ease;
-
- &:focus-within {
- border-color: ${({ theme }) =>
- theme.BACKGROUND_SECONDARY === "#f2f3f5"
- ? "rgba(59, 130, 246, 0.6)"
- : "rgba(96, 165, 250, 0.6)"};
- }
`;
const Counter = styled.div<{ $none?: boolean }>`
@@ -58,144 +32,91 @@ const Counter = styled.div<{ $none?: boolean }>`
letter-spacing: 0.2px;
`;
-const Hint = styled.div`
- display: flex;
- justify-content: space-between;
- font-size: 10.5px;
- color: ${({ theme }) =>
- theme.BACKGROUND_SECONDARY === "#f2f3f5"
- ? "rgba(15, 23, 42, 0.5)"
- : "rgba(255, 255, 255, 0.45)"};
- padding: 0 2px;
- letter-spacing: 0.2px;
-
- kbd {
- display: inline-block;
- padding: 1px 5px;
- margin: 0 2px;
- font-size: 10px;
- font-family: inherit;
- color: inherit;
- background: ${({ theme }) =>
- theme.BACKGROUND_SECONDARY === "#f2f3f5"
- ? "rgba(15, 23, 42, 0.06)"
- : "rgba(255, 255, 255, 0.08)"};
- border: 1px solid
- ${({ theme }) =>
- theme.BACKGROUND_SECONDARY === "#f2f3f5"
- ? "rgba(15, 23, 42, 0.1)"
- : "rgba(255, 255, 255, 0.08)"};
- border-radius: 3px;
- line-height: 1;
- }
-`;
-
export const SearchInput = ({ onClose }: SearchInputProps) => {
const { value, setValue, next, prev, clear, nodeCount, selectedNode } = useFocusNode();
- const os = useOs();
const inputRef = React.useRef<HTMLInputElement>(null);
- const coreKey = os === "macos" ? "⌘" : "Ctrl";
const hasValue = value.length > 0;
const hasMatches = nodeCount > 0;
const noResults = hasValue && !hasMatches;
React.useEffect(() => {
- inputRef.current?.focus();
+ inputRef.current?.focus({ preventScroll: true });
}, []);
- const handleClear = () => {
+ const handleClose = () => {
clear();
- inputRef.current?.focus();
- };
-
- const handleEscape = () => {
- if (hasValue) {
- clear();
- return;
- }
onClose?.();
};
return (
- <Wrapper>
- <Row>
- <AiOutlineSearch size={14} opacity={0.6} />
- <TextInput
- ref={inputRef}
- variant="unstyled"
- type="search"
- size="xs"
- id="search-node"
- flex={1}
- value={value}
- onChange={e => setValue(e.currentTarget.value)}
- placeholder={`Search nodes (${coreKey}+F)`}
- autoComplete="off"
- autoCorrect="off"
- spellCheck={false}
- onKeyDown={getHotkeyHandler([
- ["Enter", next],
- ["shift+Enter", prev],
- ["Escape", handleEscape],
- ])}
- styles={{
- input: { minHeight: 26, height: 26, padding: "0 4px" },
- }}
- />
- {hasValue && (
- <Counter $none={noResults}>
- {noResults ? "No matches" : `${selectedNode + 1} / ${nodeCount}`}
- </Counter>
- )}
- <Tooltip label="Previous (⇧⏎)" position="top" withArrow openDelay={500}>
- <ActionIcon
- aria-label="previous match"
- size="sm"
- radius="sm"
- variant="subtle"
- color="gray"
- onClick={prev}
- disabled={!hasMatches}
- >
- <LuChevronUp size={14} />
- </ActionIcon>
- </Tooltip>
- <Tooltip label="Next (⏎)" position="top" withArrow openDelay={500}>
- <ActionIcon
- aria-label="next match"
- size="sm"
- radius="sm"
- variant="subtle"
- color="gray"
- onClick={next}
- disabled={!hasMatches}
- >
- <LuChevronDown size={14} />
- </ActionIcon>
- </Tooltip>
- <Tooltip label="Clear (Esc)" position="top" withArrow openDelay={500}>
- <ActionIcon
- aria-label="clear search"
- size="sm"
- radius="sm"
- variant="subtle"
- color="gray"
- onClick={handleClear}
- disabled={!hasValue}
- >
- <LuX size={14} />
- </ActionIcon>
- </Tooltip>
- </Row>
- <Hint>
- <Text component="span" inherit>
- <kbd>↵</kbd> next <kbd>⇧ ↵</kbd> prev
- </Text>
- <Text component="span" inherit>
- <kbd>Esc</kbd> close
- </Text>
- </Hint>
- </Wrapper>
+ <Row>
+ <AiOutlineSearch size={14} opacity={0.6} />
+ <TextInput
+ ref={inputRef}
+ variant="unstyled"
+ type="search"
+ size="xs"
+ id="search-node"
+ flex={1}
+ value={value}
+ onChange={e => setValue(e.currentTarget.value)}
+ placeholder="Search nodes"
+ autoComplete="off"
+ autoCorrect="off"
+ spellCheck={false}
+ onKeyDown={getHotkeyHandler([
+ ["Enter", next],
+ ["shift+Enter", prev],
+ ["Escape", handleClose],
+ ])}
+ styles={{
+ input: { minHeight: 26, height: 26, padding: "0 4px" },
+ }}
+ />
+ {hasValue && (
+ <Counter $none={noResults}>
+ {noResults ? "No matches" : `${selectedNode + 1} / ${nodeCount}`}
+ </Counter>
+ )}
+ <Tooltip label="Previous (⇧⏎)" position="top" withArrow openDelay={500}>
+ <ActionIcon
+ aria-label="previous match"
+ size="sm"
+ radius="sm"
+ variant="subtle"
+ color="gray"
+ onClick={prev}
+ disabled={!hasMatches}
+ >
+ <LuChevronUp size={14} />
+ </ActionIcon>
+ </Tooltip>
+ <Tooltip label="Next (⏎)" position="top" withArrow openDelay={500}>
+ <ActionIcon
+ aria-label="next match"
+ size="sm"
+ radius="sm"
+ variant="subtle"
+ color="gray"
+ onClick={next}
+ disabled={!hasMatches}
+ >
+ <LuChevronDown size={14} />
+ </ActionIcon>
+ </Tooltip>
+ <Tooltip label="Close (Esc)" position="top" withArrow openDelay={500}>
+ <ActionIcon
+ aria-label="close search"
+ size="sm"
+ radius="sm"
+ variant="subtle"
+ color="gray"
+ onClick={handleClose}
+ >
+ <LuX size={14} />
+ </ActionIcon>
+ </Tooltip>
+ </Row>
);
};
diff --git a/apps/www/src/features/editor/views/GraphView/Toolbar/index.tsx b/apps/www/src/features/editor/views/GraphView/Toolbar/index.tsx
index b565e47a..9794dad3 100644
--- a/apps/www/src/features/editor/views/GraphView/Toolbar/index.tsx
+++ b/apps/www/src/features/editor/views/GraphView/Toolbar/index.tsx
@@ -1,7 +1,7 @@
import React from "react";
-import { ActionIcon, Divider, Flex, Menu, Popover, Tooltip } from "@mantine/core";
+import { ActionIcon, Divider, Flex, Menu, Tooltip } from "@mantine/core";
import { useHotkeys } from "@mantine/hooks";
-import styled from "styled-components";
+import styled, { css } from "styled-components";
import type { LayoutDirection } from "jsoncrack-react";
import { event as gaEvent } from "nextjs-google-analytics";
import { BsCheck2 } from "react-icons/bs";
@@ -23,15 +23,7 @@ import { useModal } from "../../../../../store/useModal";
import { SearchInput } from "../../../Toolbar/SearchInput";
import useGraph from "../stores/useGraph";
-const StyledToolbar = styled.div`
- position: absolute;
- bottom: 12px;
- left: 50%;
- transform: translateX(-50%);
- z-index: 3;
- display: flex;
- align-items: center;
- padding: 5px;
+const glassSurface = css`
border-radius: 14px;
background: ${({ theme }) =>
theme.BACKGROUND_SECONDARY === "#f2f3f5"
@@ -57,6 +49,31 @@ const StyledToolbar = styled.div`
theme.BACKGROUND_SECONDARY === "#f2f3f5"
? "rgba(15, 23, 42, 0.12)"
: "rgba(0, 0, 0, 0.45)"};
+`;
+
+const StyledToolbarDock = styled.div`
+ position: absolute;
+ bottom: 12px;
+ left: 50%;
+ transform: translateX(-50%);
+ z-index: 3;
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ gap: 8px;
+`;
+
+const StyledSearchBar = styled.div`
+ ${glassSurface}
+ padding: 6px 8px;
+ min-width: 360px;
+`;
+
+const StyledToolbar = styled.div`
+ ${glassSurface}
+ display: flex;
+ align-items: center;
+ padding: 5px;
[data-mantine-divider] {
border-color: ${({ theme }) =>
@@ -103,12 +120,8 @@ export const Toolbar = () => {
}
}, []);
- const handleSearchClick = () => {
- setSearchOpen(true);
- requestAnimationFrame(() => {
- const input = document.querySelector("#search-node") as HTMLInputElement | null;
- input?.focus();
- });
+ const handleSearchToggle = () => {
+ setSearchOpen(current => !current);
};
const toggleDirection = () => {
@@ -122,7 +135,7 @@ export const Toolbar = () => {
["mod+[minus]", zoomOut, { usePhysicalKeys: true }],
["shift+Digit1", focusFirstNode, { usePhysicalKeys: true }],
["shift+Digit2", centerView, { usePhysicalKeys: true }],
- ["mod+f", handleSearchClick],
+ ["mod+f", handleSearchToggle],
["mod+s", () => setVisible("DownloadModal", true)],
["mod+shift+d", toggleDirection],
],
@@ -130,210 +143,196 @@ export const Toolbar = () => {
);
return (
- <StyledToolbar>
- <Flex gap={4} align="center">
- <Tooltip label="Center first item (⇧1)" position="top" withArrow openDelay={750}>
+ <StyledToolbarDock>
+ {searchOpen && (
+ <StyledSearchBar>
+ <SearchInput onClose={() => setSearchOpen(false)} />
+ </StyledSearchBar>
+ )}
+ <StyledToolbar>
+ <Flex gap={4} align="center">
+ <Tooltip label="Center first item (⇧1)" position="top" withArrow openDelay={750}>
+ <ActionIcon
+ aria-label="center first item"
+ size="lg"
+ radius="md"
+ variant="subtle"
+ color="gray"
+ onClick={() => {
+ focusFirstNode();
+ gaEvent("focus_first_node");
+ }}
+ >
+ <MdOutlineCenterFocusStrong size={18} />
+ </ActionIcon>
+ </Tooltip>
+
+ <Tooltip label="Fit to center (⇧2)" position="top" withArrow openDelay={750}>
+ <ActionIcon
+ aria-label="fit to center"
+ size="lg"
+ radius="md"
+ variant="subtle"
+ color="gray"
+ onClick={() => {
+ centerView();
+ gaEvent("center_view");
+ }}
+ >
+ <MdFullscreen size={20} />
+ </ActionIcon>
+ </Tooltip>
+
<ActionIcon
- aria-label="center first item"
+ aria-label="zoom out"
size="lg"
radius="md"
variant="subtle"
color="gray"
onClick={() => {
- focusFirstNode();
- gaEvent("focus_first_node");
+ zoomOut();
+ gaEvent("zoom_out");
}}
>
- <MdOutlineCenterFocusStrong size={18} />
+ <LuMinus size={16} />
</ActionIcon>
- </Tooltip>
- <Tooltip label="Fit to center (⇧2)" position="top" withArrow openDelay={750}>
<ActionIcon
- aria-label="fit to center"
+ aria-label="zoom in"
size="lg"
radius="md"
variant="subtle"
color="gray"
onClick={() => {
- centerView();
- gaEvent("center_view");
+ zoomIn();
+ gaEvent("zoom_in");
}}
>
- <MdFullscreen size={20} />
+ <LuPlus size={16} />
</ActionIcon>
- </Tooltip>
- <ActionIcon
- aria-label="zoom out"
- size="lg"
- radius="md"
- variant="subtle"
- color="gray"
- onClick={() => {
- zoomOut();
- gaEvent("zoom_out");
- }}
- >
- <LuMinus size={16} />
- </ActionIcon>
+ <Divider orientation="vertical" mx={2} />
- <ActionIcon
- aria-label="zoom in"
- size="lg"
- radius="md"
- variant="subtle"
- color="gray"
- onClick={() => {
- zoomIn();
- gaEvent("zoom_in");
- }}
- >
- <LuPlus size={16} />
- </ActionIcon>
+ <Tooltip label={`Export (${coreKey}+S)`} position="top" withArrow openDelay={750}>
+ <ActionIcon
+ aria-label="export"
+ size="lg"
+ radius="md"
+ variant="subtle"
+ color="gray"
+ onClick={() => setVisible("DownloadModal", true)}
+ >
+ <LuImageDown size={18} />
+ </ActionIcon>
+ </Tooltip>
- <Divider orientation="vertical" mx={2} />
+ <Tooltip label={`Search (${coreKey}+F)`} position="top" withArrow openDelay={750}>
+ <ActionIcon
+ aria-label="search"
+ size="lg"
+ radius="md"
+ variant={searchOpen ? "light" : "subtle"}
+ color="gray"
+ onClick={handleSearchToggle}
+ >
+ <LuSearch size={18} />
+ </ActionIcon>
+ </Tooltip>
- <Tooltip label={`Export (${coreKey}+S)`} position="top" withArrow openDelay={750}>
- <ActionIcon
- aria-label="export"
- size="lg"
- radius="md"
- variant="subtle"
- color="gray"
- onClick={() => setVisible("DownloadModal", true)}
+ <Tooltip label="Rotate layout" position="top" withArrow openDelay={750}>
+ <ActionIcon
+ aria-label="rotate layout"
+ size="lg"
+ radius="md"
+ variant="subtle"
+ color="gray"
+ onClick={toggleDirection}
+ >
+ <TbArrowsLeftRight size={18} />
+ </ActionIcon>
+ </Tooltip>
+
+ <Tooltip
+ label={
+ collapsedPaths.length > 0
+ ? `Expand all (${collapsedPaths.length} collapsed)`
+ : "Collapse all"
+ }
+ position="top"
+ withArrow
+ openDelay={750}
>
- <LuImageDown size={18} />
- </ActionIcon>
- </Tooltip>
+ <ActionIcon
+ aria-label={collapsedPaths.length > 0 ? "expand all" : "collapse all"}
+ size="lg"
+ radius="md"
+ variant="subtle"
+ color="gray"
+ onClick={() => {
+ if (collapsedPaths.length > 0) {
+ expandAll();
+ gaEvent("expand_all");
+ } else {
+ collapseAllDepth1(json);
+ gaEvent("collapse_all");
+ }
+ }}
+ >
+ {collapsedPaths.length > 0 ? <LuCopyPlus size={18} /> : <LuCopyMinus size={18} />}
+ </ActionIcon>
+ </Tooltip>
- <Popover
- opened={searchOpen}
- onChange={setSearchOpen}
- position="top"
- withArrow
- shadow="md"
- trapFocus={false}
- closeOnClickOutside
- >
- <Popover.Target>
- <Tooltip label={`Search (${coreKey}+F)`} position="top" withArrow openDelay={750}>
- <ActionIcon
- aria-label="search"
- size="lg"
- radius="md"
- variant="subtle"
- color="gray"
+ <Divider orientation="vertical" mx={2} />
+
+ <Menu trigger="click" position="top-end">
+ <Menu.Target>
+ <Tooltip label="Preferences" position="top" withArrow openDelay={750}>
+ <ActionIcon
+ aria-label="preferences"
+ size="lg"
+ radius="md"
+ variant="subtle"
+ color="gray"
+ >
+ <LuSettings2 size={18} />
+ </ActionIcon>
+ </Tooltip>
+ </Menu.Target>
+ <Menu.Dropdown>
+ <Menu.Item
+ fz="sm"
+ leftSection={darkmodeEnabled ? <FaSun /> : <FaMoon />}
+ onClick={() => toggleDarkMode(!darkmodeEnabled)}
+ closeMenuOnClick={false}
+ >
+ {darkmodeEnabled ? "Light Mode" : "Dark Mode"}
+ </Menu.Item>
+ <Menu.Item
+ fz="sm"
+ rightSection={<BsCheck2 display={gesturesEnabled ? "initial" : "none"} />}
onClick={() => {
- if (searchOpen) {
- setSearchOpen(false);
- } else {
- handleSearchClick();
- }
+ toggleGestures(!gesturesEnabled);
+ gaEvent("toggle_gestures", { label: gesturesEnabled ? "on" : "off" });
}}
+ closeMenuOnClick={false}
>
- <LuSearch size={18} />
- </ActionIcon>
- </Tooltip>
- </Popover.Target>
- <Popover.Dropdown p={8}>
- <SearchInput onClose={() => setSearchOpen(false)} />
- </Popover.Dropdown>
- </Popover>
-
- <Tooltip label="Rotate layout" position="top" withArrow openDelay={750}>
- <ActionIcon
- aria-label="rotate layout"
- size="lg"
- radius="md"
- variant="subtle"
- color="gray"
- onClick={toggleDirection}
- >
- <TbArrowsLeftRight size={18} />
- </ActionIcon>
- </Tooltip>
-
- <Tooltip
- label={
- collapsedPaths.length > 0
- ? `Expand all (${collapsedPaths.length} collapsed)`
- : "Collapse all"
- }
- position="top"
- withArrow
- openDelay={750}
- >
- <ActionIcon
- aria-label={collapsedPaths.length > 0 ? "expand all" : "collapse all"}
- size="lg"
- radius="md"
- variant="subtle"
- color="gray"
- onClick={() => {
- if (collapsedPaths.length > 0) {
- expandAll();
- gaEvent("expand_all");
- } else {
- collapseAllDepth1(json);
- gaEvent("collapse_all");
- }
- }}
- >
- {collapsedPaths.length > 0 ? <LuCopyPlus size={18} /> : <LuCopyMinus size={18} />}
- </ActionIcon>
- </Tooltip>
-
- <Divider orientation="vertical" mx={2} />
-
- <Menu trigger="click" position="top-end">
- <Menu.Target>
- <Tooltip label="Preferences" position="top" withArrow openDelay={750}>
- <ActionIcon
- aria-label="preferences"
- size="lg"
- radius="md"
- variant="subtle"
- color="gray"
+ Zoom on Scroll
+ </Menu.Item>
+ <Menu.Item
+ fz="sm"
+ rightSection={<BsCheck2 display={rulersEnabled ? "initial" : "none"} />}
+ onClick={() => {
+ toggleRulers(!rulersEnabled);
+ gaEvent("toggle_rulers", { label: rulersEnabled ? "on" : "off" });
+ }}
+ closeMenuOnClick={false}
>
- <LuSettings2 size={18} />
- </ActionIcon>
- </Tooltip>
- </Menu.Target>
- <Menu.Dropdown>
- <Menu.Item
- fz="sm"
- leftSection={darkmodeEnabled ? <FaSun /> : <FaMoon />}
- onClick={() => toggleDarkMode(!darkmodeEnabled)}
- closeMenuOnClick={false}
- >
- {darkmodeEnabled ? "Light Mode" : "Dark Mode"}
- </Menu.Item>
- <Menu.Item
- fz="sm"
- rightSection={<BsCheck2 display={gesturesEnabled ? "initial" : "none"} />}
- onClick={() => {
- toggleGestures(!gesturesEnabled);
- gaEvent("toggle_gestures", { label: gesturesEnabled ? "on" : "off" });
- }}
- closeMenuOnClick={false}
- >
- Zoom on Scroll
- </Menu.Item>
- <Menu.Item
- fz="sm"
- rightSection={<BsCheck2 display={rulersEnabled ? "initial" : "none"} />}
- onClick={() => {
- toggleRulers(!rulersEnabled);
- gaEvent("toggle_rulers", { label: rulersEnabled ? "on" : "off" });
- }}
- closeMenuOnClick={false}
- >
- Rulers
- </Menu.Item>
- </Menu.Dropdown>
- </Menu>
- </Flex>
- </StyledToolbar>
+ Rulers
+ </Menu.Item>
+ </Menu.Dropdown>
+ </Menu>
+ </Flex>
+ </StyledToolbar>
+ </StyledToolbarDock>
);
};