@noya-app/noya-designsystem 0.1.17 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noya-app/noya-designsystem",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
@@ -36,6 +36,7 @@
36
36
  "@radix-ui/react-utils": "^0.0.5",
37
37
  "immer": "9.0.5",
38
38
  "kiwi.js": "^1.1.3",
39
+ "react-resizable-panels": "2.0.16",
39
40
  "react-virtualized": "^9.22.3",
40
41
  "react-window": "^1.8.8",
41
42
  "vscode-fuzzy-scorer": "^0.0.4"
@@ -0,0 +1,63 @@
1
+ import {
2
+ Divider,
3
+ ScrollArea,
4
+ Stack,
5
+ useDesignSystemTheme,
6
+ withSeparatorElements,
7
+ } from "@noya-app/noya-designsystem";
8
+ import React, { forwardRef, memo } from "react";
9
+
10
+ export const InspectorContainer = memo(
11
+ forwardRef(function InspectorContainer(
12
+ {
13
+ header,
14
+ children,
15
+ fallback,
16
+ showDividers,
17
+ id,
18
+ className,
19
+ style,
20
+ }: {
21
+ header?: React.ReactNode;
22
+ children?: React.ReactNode;
23
+ fallback?: React.ReactNode;
24
+ showDividers?: boolean;
25
+ id?: string;
26
+ className?: string;
27
+ style?: React.CSSProperties;
28
+ },
29
+ forwardedRef: React.ForwardedRef<HTMLDivElement>
30
+ ) {
31
+ const theme = useDesignSystemTheme();
32
+
33
+ return (
34
+ <div
35
+ ref={forwardedRef}
36
+ id={id}
37
+ className={className}
38
+ style={{
39
+ display: "flex",
40
+ flexDirection: "column",
41
+ position: "relative",
42
+ background: theme.colors.sidebar.background,
43
+ ...style,
44
+ }}
45
+ >
46
+ {header}
47
+ {children ? (
48
+ <ScrollArea>
49
+ <Stack.V position="relative">
50
+ {showDividers
51
+ ? withSeparatorElements(children, <Divider />)
52
+ : children}
53
+ </Stack.V>
54
+ </ScrollArea>
55
+ ) : fallback ? (
56
+ <Stack.V position="relative" height="100%">
57
+ {fallback}
58
+ </Stack.V>
59
+ ) : null}
60
+ </div>
61
+ );
62
+ })
63
+ );
@@ -0,0 +1,212 @@
1
+ import { Stack, useDesignSystemTheme } from "@noya-app/noya-designsystem";
2
+ import { useKeyboardShortcuts } from "@noya-app/noya-keymap";
3
+ import React, { forwardRef, useImperativeHandle, useRef } from "react";
4
+ import {
5
+ ImperativePanelGroupHandle,
6
+ ImperativePanelHandle,
7
+ Panel,
8
+ PanelGroup,
9
+ PanelResizeHandle,
10
+ } from "react-resizable-panels";
11
+ import {
12
+ CONTENT_AREA_ID,
13
+ EDITOR_PANEL_GROUP_ID,
14
+ LEFT_SIDEBAR_ID,
15
+ PanelLayoutState,
16
+ RIGHT_SIDEBAR_ID,
17
+ usePreservePanelSize,
18
+ } from "../hooks/usePreservePanelSize";
19
+ import { useWindowSize } from "../hooks/useWindowSize";
20
+
21
+ interface Props {
22
+ autoSavePrefix?: string;
23
+ leftSidebarContent?: React.ReactNode;
24
+ children?: React.ReactNode;
25
+ rightSidebarContent?: React.ReactNode;
26
+ hasRightSidebar?: boolean;
27
+ hasLeftSidebar?: boolean;
28
+ onChangeLayoutState?: (layoutState: PanelLayoutState) => void;
29
+ leftSidebarInitialSize?: number;
30
+ rightSidebarInitialSize?: number;
31
+ leftSidebarCanResize?: boolean;
32
+ }
33
+
34
+ export type IWorkspaceLayout = {
35
+ setLeftSidebarExpanded: (expanded: boolean) => void;
36
+ setRightSidebarExpanded: (expanded: boolean) => void;
37
+ toggleAllSidebars: () => void;
38
+ toggleLeftSidebar: () => void;
39
+ toggleRightSidebar: () => void;
40
+ };
41
+
42
+ export const WorkspaceLayout = forwardRef(function WorkspaceLayout(
43
+ {
44
+ autoSavePrefix,
45
+ leftSidebarContent: leftPanelContent,
46
+ children: centerPanelContent,
47
+ rightSidebarContent: rightPanelContent,
48
+ hasLeftSidebar = !!leftPanelContent,
49
+ hasRightSidebar = !!rightPanelContent,
50
+ onChangeLayoutState,
51
+ leftSidebarInitialSize = 280,
52
+ rightSidebarInitialSize = 400,
53
+ leftSidebarCanResize = false,
54
+ }: Props,
55
+ forwardedRef: React.ForwardedRef<IWorkspaceLayout>
56
+ ) {
57
+ const windowSize = useWindowSize();
58
+ const leftSidebarPercentage =
59
+ (leftSidebarInitialSize / windowSize.width) * 100;
60
+ const rightSidebarPercentage =
61
+ (rightSidebarInitialSize / windowSize.width) * 100;
62
+ const panelGroupRef = useRef<ImperativePanelGroupHandle>(null);
63
+ const leftSidebarRef = useRef<ImperativePanelHandle>(null);
64
+ const rightSidebarRef = useRef<ImperativePanelHandle>(null);
65
+
66
+ usePreservePanelSize(panelGroupRef, onChangeLayoutState);
67
+
68
+ const theme = useDesignSystemTheme();
69
+
70
+ useImperativeHandle(forwardedRef, () => ({
71
+ setLeftSidebarExpanded: (expanded: boolean) => {
72
+ if (expanded) {
73
+ leftSidebarRef.current?.expand();
74
+ } else {
75
+ leftSidebarRef.current?.collapse();
76
+ }
77
+ },
78
+ setRightSidebarExpanded: (expanded: boolean) => {
79
+ if (expanded) {
80
+ rightSidebarRef.current?.expand();
81
+ } else {
82
+ rightSidebarRef.current?.collapse();
83
+ }
84
+ },
85
+ toggleAllSidebars: () => {
86
+ const isLeftSidebarExpanded = leftSidebarRef.current?.isExpanded();
87
+ const isRightSidebarExpanded = rightSidebarRef.current?.isExpanded();
88
+
89
+ if (isLeftSidebarExpanded || isRightSidebarExpanded) {
90
+ leftSidebarRef.current?.collapse();
91
+ rightSidebarRef.current?.collapse();
92
+ } else {
93
+ leftSidebarRef.current?.expand();
94
+ rightSidebarRef.current?.expand();
95
+ }
96
+ },
97
+ toggleLeftSidebar: () => {
98
+ const isLeftSidebarExpanded = leftSidebarRef.current?.isExpanded();
99
+
100
+ if (isLeftSidebarExpanded) {
101
+ leftSidebarRef.current?.collapse();
102
+ } else {
103
+ leftSidebarRef.current?.expand();
104
+ }
105
+ },
106
+ toggleRightSidebar: () => {
107
+ const isRightSidebarExpanded = rightSidebarRef.current?.isExpanded();
108
+
109
+ if (isRightSidebarExpanded) {
110
+ rightSidebarRef.current?.collapse();
111
+ } else {
112
+ rightSidebarRef.current?.expand();
113
+ }
114
+ },
115
+ }));
116
+
117
+ useKeyboardShortcuts({
118
+ "Mod-.": () => {
119
+ const isLeftSidebarExpanded = leftSidebarRef.current?.isExpanded();
120
+ const isRightSidebarExpanded = rightSidebarRef.current?.isExpanded();
121
+
122
+ if (isLeftSidebarExpanded || isRightSidebarExpanded) {
123
+ leftSidebarRef.current?.collapse();
124
+ rightSidebarRef.current?.collapse();
125
+ } else {
126
+ leftSidebarRef.current?.expand();
127
+ rightSidebarRef.current?.expand();
128
+ }
129
+ },
130
+ });
131
+
132
+ return (
133
+ <Stack.H flex="1">
134
+ <PanelGroup
135
+ ref={panelGroupRef}
136
+ id={EDITOR_PANEL_GROUP_ID}
137
+ direction="horizontal"
138
+ autoSaveId={
139
+ autoSavePrefix
140
+ ? `${autoSavePrefix}--${EDITOR_PANEL_GROUP_ID}`
141
+ : undefined
142
+ }
143
+ style={{ flex: "1" }}
144
+ >
145
+ {hasLeftSidebar && (
146
+ <>
147
+ <Panel
148
+ id={LEFT_SIDEBAR_ID}
149
+ order={1}
150
+ ref={leftSidebarRef}
151
+ defaultSize={leftSidebarPercentage}
152
+ minSize={leftSidebarPercentage}
153
+ {...(!leftSidebarCanResize && { maxSize: leftSidebarPercentage })}
154
+ collapsible
155
+ style={{
156
+ display: "flex",
157
+ flexDirection: "row",
158
+ }}
159
+ >
160
+ {leftPanelContent}
161
+ </Panel>
162
+ <PanelResizeHandle
163
+ style={{
164
+ cursor: "col-resize",
165
+ width: "1px",
166
+ height: "100%",
167
+ backgroundColor: theme.colors.divider,
168
+ }}
169
+ />
170
+ </>
171
+ )}
172
+ <Panel
173
+ id={CONTENT_AREA_ID}
174
+ order={2}
175
+ minSize={10}
176
+ style={{
177
+ display: "flex",
178
+ flexDirection: "row",
179
+ }}
180
+ >
181
+ {centerPanelContent}
182
+ </Panel>
183
+ {hasRightSidebar && (
184
+ <>
185
+ <PanelResizeHandle
186
+ style={{
187
+ cursor: "col-resize",
188
+ width: "1px",
189
+ height: "100%",
190
+ backgroundColor: theme.colors.divider,
191
+ }}
192
+ />
193
+ <Panel
194
+ id={RIGHT_SIDEBAR_ID}
195
+ order={3}
196
+ ref={rightSidebarRef}
197
+ minSize={rightSidebarPercentage}
198
+ defaultSize={rightSidebarPercentage}
199
+ collapsible
200
+ style={{
201
+ display: "flex",
202
+ flexDirection: "row",
203
+ }}
204
+ >
205
+ {rightPanelContent}
206
+ </Panel>
207
+ </>
208
+ )}
209
+ </PanelGroup>
210
+ </Stack.H>
211
+ );
212
+ });
@@ -11,6 +11,7 @@ import React, {
11
11
  import { ThemeProvider, useTheme } from "styled-components";
12
12
  import { ToastProvider } from "../components/Toast";
13
13
  import { Theme } from "../theme";
14
+ import * as darkTheme from "../theme/dark";
14
15
  import * as lightTheme from "../theme/light";
15
16
  import { DialogProvider } from "./DialogContext";
16
17
  import { FloatingWindowProvider } from "./FloatingWindowContext";
@@ -54,11 +55,11 @@ const DesignSystemConfigurationContext =
54
55
  export const DesignSystemConfigurationProvider = memo(
55
56
  function DesignSystemConfigurationProvider({
56
57
  children,
57
- theme = lightTheme,
58
+ theme,
58
59
  platform,
59
60
  }: {
60
61
  children: ReactNode;
61
- theme?: Theme;
62
+ theme?: Theme | "light" | "dark";
62
63
  platform?: PlatformName;
63
64
  }) {
64
65
  const [internalPlatform, setInternalPlatform] = useState<PlatformName>(
@@ -78,9 +79,16 @@ export const DesignSystemConfigurationProvider = memo(
78
79
  [platform, internalPlatform]
79
80
  );
80
81
 
82
+ const resolvedTheme =
83
+ theme === "light"
84
+ ? lightTheme
85
+ : theme === "dark"
86
+ ? darkTheme
87
+ : theme ?? lightTheme;
88
+
81
89
  return (
82
90
  <DesignSystemConfigurationContext.Provider value={contextValue}>
83
- <ThemeProvider theme={theme}>
91
+ <ThemeProvider theme={resolvedTheme}>
84
92
  <DialogProvider>
85
93
  <ToastProvider>
86
94
  <FloatingWindowProvider>{children}</FloatingWindowProvider>
@@ -0,0 +1,156 @@
1
+ import React, { useLayoutEffect, useRef } from "react";
2
+ import { ImperativePanelGroupHandle } from "react-resizable-panels";
3
+ import { useWindowSize } from "./useWindowSize";
4
+
5
+ export const EDITOR_PANEL_GROUP_ID = "editor-panel-group";
6
+ export const LEFT_SIDEBAR_ID = "editor-left-sidebar";
7
+ export const RIGHT_SIDEBAR_ID = "editor-right-sidebar";
8
+ export const CONTENT_AREA_ID = "editor-content-area";
9
+
10
+ export type PanelLayoutState = {
11
+ leftSidebarCollapsed: boolean;
12
+ rightSidebarCollapsed: boolean;
13
+ };
14
+
15
+ export function usePreservePanelSize(
16
+ panelGroupRef: React.RefObject<ImperativePanelGroupHandle | null>,
17
+ setPanelLayoutState?: (state: PanelLayoutState) => void
18
+ ) {
19
+ const windowSize = useWindowSize();
20
+ const layoutRef = useRef<
21
+ { windowWidth: number; panelPercentages: number[] } | undefined
22
+ >();
23
+
24
+ useLayoutEffect(() => {
25
+ const panelGroup = document.querySelector(
26
+ `[data-panel-group-id="${EDITOR_PANEL_GROUP_ID}"]`
27
+ ) as HTMLElement;
28
+
29
+ function observePanels() {
30
+ const panels = [
31
+ ...panelGroup.querySelectorAll<HTMLElement>("[data-panel]"),
32
+ ];
33
+
34
+ const panelObserver = new ResizeObserver(() => {
35
+ const layout = {
36
+ windowWidth: panelGroup.offsetWidth,
37
+ panelPercentages: panelGroupRef.current?.getLayout() ?? [],
38
+ };
39
+ layoutRef.current = layout;
40
+
41
+ const leftSidebar = panels.find(
42
+ (panel) => panel.id === LEFT_SIDEBAR_ID
43
+ );
44
+ const rightSidebar = panels.find(
45
+ (panel) => panel.id === RIGHT_SIDEBAR_ID
46
+ );
47
+
48
+ setPanelLayoutState?.({
49
+ leftSidebarCollapsed: leftSidebar?.offsetWidth === 0,
50
+ rightSidebarCollapsed: rightSidebar?.offsetWidth === 0,
51
+ });
52
+ });
53
+
54
+ panels.forEach((panel) => panelObserver.observe(panel));
55
+
56
+ return () => {
57
+ panelObserver.disconnect();
58
+ };
59
+ }
60
+
61
+ let disconnectPanelObserver = observePanels();
62
+
63
+ const panelGroupObserver = new MutationObserver(() => {
64
+ disconnectPanelObserver();
65
+ disconnectPanelObserver = observePanels();
66
+ });
67
+
68
+ panelGroupObserver.observe(panelGroup, {
69
+ childList: true,
70
+ });
71
+
72
+ return () => {
73
+ disconnectPanelObserver();
74
+ panelGroupObserver.disconnect();
75
+ };
76
+ }, [panelGroupRef, setPanelLayoutState]);
77
+
78
+ useLayoutEffect(() => {
79
+ if (!layoutRef.current) return;
80
+
81
+ // Preserve the pixel values of the left and right sidebar when the window is resized.
82
+ // To do this we convert the percentage values to pixel values based on the old window width,
83
+ // then convert the pixel values to percentage values based on the new window width.
84
+ const {
85
+ windowWidth: oldWindowWidth,
86
+ panelPercentages: oldPanelPercentages,
87
+ } = layoutRef.current;
88
+
89
+ // Check the types of the values in the array
90
+ if (
91
+ oldPanelPercentages.some(
92
+ (value) => typeof value !== "number" || isNaN(value)
93
+ ) ||
94
+ oldWindowWidth === 0
95
+ ) {
96
+ return;
97
+ }
98
+
99
+ const currentLayout = panelGroupRef.current?.getLayout();
100
+
101
+ // Ensure we have the same number of panels
102
+ if (!currentLayout || currentLayout.length !== oldPanelPercentages.length) {
103
+ return;
104
+ }
105
+
106
+ if (currentLayout.length === 3) {
107
+ const leftSidebarWidth = (oldPanelPercentages[0] / 100) * oldWindowWidth;
108
+ const rightSidebarWidth = (oldPanelPercentages[2] / 100) * oldWindowWidth;
109
+
110
+ const newLeftSidebarPercentage =
111
+ (leftSidebarWidth / windowSize.width) * 100;
112
+ const newRightSidebarPercentage =
113
+ (rightSidebarWidth / windowSize.width) * 100;
114
+
115
+ const newLayout = [
116
+ newLeftSidebarPercentage,
117
+ 100 - newLeftSidebarPercentage - newRightSidebarPercentage,
118
+ newRightSidebarPercentage,
119
+ ];
120
+
121
+ try {
122
+ panelGroupRef.current?.setLayout(newLayout);
123
+ } catch (e) {
124
+ console.error(e);
125
+ console.info("snapshot on error", {
126
+ oldWindowWidth,
127
+ oldPanelPercentages,
128
+ newLayout,
129
+ currentLayout,
130
+ });
131
+ }
132
+ } else if (currentLayout.length === 2) {
133
+ const leftSidebarWidth = (oldPanelPercentages[0] / 100) * oldWindowWidth;
134
+
135
+ const newLeftSidebarPercentage =
136
+ (leftSidebarWidth / windowSize.width) * 100;
137
+
138
+ const newLayout = [
139
+ newLeftSidebarPercentage,
140
+ 100 - newLeftSidebarPercentage,
141
+ ];
142
+
143
+ try {
144
+ panelGroupRef.current?.setLayout(newLayout);
145
+ } catch (e) {
146
+ console.error(e);
147
+ console.info("snapshot on error", {
148
+ oldWindowWidth,
149
+ oldPanelPercentages,
150
+ newLayout,
151
+ currentLayout,
152
+ });
153
+ }
154
+ }
155
+ }, [panelGroupRef, windowSize.width]);
156
+ }
@@ -0,0 +1,26 @@
1
+ import { useEffect, useState } from "react";
2
+
3
+ export function useWindowSize() {
4
+ const [size, setSize] = useState(
5
+ typeof window === "undefined"
6
+ ? { width: 0, height: 0 }
7
+ : {
8
+ width: window.innerWidth,
9
+ height: window.innerHeight,
10
+ }
11
+ );
12
+
13
+ useEffect(() => {
14
+ const handleResize = () =>
15
+ setSize({
16
+ width: window.innerWidth,
17
+ height: window.innerHeight,
18
+ });
19
+
20
+ window.addEventListener("resize", handleResize);
21
+
22
+ return () => window.removeEventListener("resize", handleResize);
23
+ }, []);
24
+
25
+ return size;
26
+ }
package/src/index.tsx CHANGED
@@ -32,6 +32,7 @@ export * from "./components/GridView";
32
32
  export * from "./components/IconButton";
33
33
  export * from "./components/InputField";
34
34
  export * from "./components/InputFieldWithCompletions";
35
+ export * from "./components/InspectorContainer";
35
36
  export * from "./components/Label";
36
37
  export * from "./components/LabeledElementView";
37
38
  export * from "./components/ListView";
@@ -50,6 +51,7 @@ export * from "./components/Text";
50
51
  export * from "./components/Toast";
51
52
  export * from "./components/Tooltip";
52
53
  export * from "./components/TreeView";
54
+ export * from "./components/WorkspaceLayout";
53
55
  export { KeyboardShortcut, SEPARATOR_ITEM } from "./components/internal/Menu";
54
56
  export type {
55
57
  ExtractMenuItemType,