@dxos/react-ui 0.4.5-main.623c49f → 0.4.5-main.8a5190e

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.
@@ -14,7 +14,9 @@ import React, {
14
14
  type PropsWithChildren,
15
15
  type SetStateAction,
16
16
  useCallback,
17
+ useEffect,
17
18
  useRef,
19
+ useState,
18
20
  } from 'react';
19
21
 
20
22
  import { useMediaQuery, useForwardedRef } from '@dxos/react-hooks';
@@ -31,6 +33,7 @@ const MAIN_NAME = 'Main';
31
33
  const GENERIC_CONSUMER_NAME = 'GenericConsumer';
32
34
 
33
35
  type MainContextValue = {
36
+ resizing: boolean;
34
37
  navigationSidebarOpen: boolean;
35
38
  setNavigationSidebarOpen: Dispatch<SetStateAction<boolean | undefined>>;
36
39
  complementarySidebarOpen: boolean;
@@ -38,6 +41,7 @@ type MainContextValue = {
38
41
  };
39
42
 
40
43
  const [MainProvider, useMainContext] = createContext<MainContextValue>(MAIN_NAME, {
44
+ resizing: false,
41
45
  navigationSidebarOpen: false,
42
46
  setNavigationSidebarOpen: (nextOpen) => {
43
47
  // TODO(burdon): Standardize with other context missing errors using raise.
@@ -82,6 +86,8 @@ type MainRootProps = PropsWithChildren<{
82
86
  onComplementarySidebarOpenChange?: (nextOpen: boolean) => void;
83
87
  }>;
84
88
 
89
+ const resizeDebounce = 3000;
90
+
85
91
  const MainRoot = ({
86
92
  navigationSidebarOpen: propsNavigationSidebarOpen,
87
93
  defaultNavigationSidebarOpen,
@@ -103,6 +109,26 @@ const MainRoot = ({
103
109
  defaultProp: defaultComplementarySidebarOpen,
104
110
  onChange: onComplementarySidebarOpenChange,
105
111
  });
112
+
113
+ const [resizing, setResizing] = useState(false);
114
+ const resizeInterval = useRef<ReturnType<typeof setTimeout> | null>(null);
115
+
116
+ const handleResize = useCallback(() => {
117
+ setResizing(true);
118
+ if (resizeInterval.current) {
119
+ clearTimeout(resizeInterval.current);
120
+ }
121
+ resizeInterval.current = setTimeout(() => {
122
+ setResizing(false);
123
+ resizeInterval.current = null;
124
+ }, resizeDebounce);
125
+ }, []);
126
+
127
+ useEffect(() => {
128
+ window.addEventListener('resize', handleResize);
129
+ return () => window.removeEventListener('resize', handleResize);
130
+ }, [handleResize]);
131
+
106
132
  return (
107
133
  <MainProvider
108
134
  {...props}
@@ -112,6 +138,7 @@ const MainRoot = ({
112
138
  complementarySidebarOpen,
113
139
  setComplementarySidebarOpen,
114
140
  }}
141
+ resizing={resizing}
115
142
  >
116
143
  {children}
117
144
  </MainProvider>
@@ -127,12 +154,16 @@ const handleOpenAutoFocus = (event: Event) => {
127
154
  type MainSidebarProps = ThemedClassName<ComponentPropsWithRef<typeof DialogContent>> & {
128
155
  swipeToDismiss?: boolean;
129
156
  open: boolean;
157
+ resizing?: boolean;
130
158
  setOpen: Dispatch<SetStateAction<boolean | undefined>>;
131
159
  side: 'inline-start' | 'inline-end';
132
160
  };
133
161
 
134
162
  const MainSidebar = forwardRef<HTMLDivElement, MainSidebarProps>(
135
- ({ classNames, children, swipeToDismiss, onOpenAutoFocus, open, setOpen, side, ...props }, forwardedRef) => {
163
+ (
164
+ { classNames, children, swipeToDismiss, onOpenAutoFocus, open, resizing, setOpen, side, ...props },
165
+ forwardedRef,
166
+ ) => {
136
167
  const [isLg] = useMediaQuery('lg', { ssr: false });
137
168
  const { tx } = useThemeContext();
138
169
  const ref = useForwardedRef(forwardedRef);
@@ -148,6 +179,7 @@ const MainSidebar = forwardRef<HTMLDivElement, MainSidebarProps>(
148
179
  {...props}
149
180
  data-side={side === 'inline-end' ? 'ie' : 'is'}
150
181
  data-state={open ? 'open' : 'closed'}
182
+ data-resizing={resizing ? 'true' : 'false'}
151
183
  className={tx('main.sidebar', 'main__sidebar', {}, classNames)}
152
184
  {...(!open && { inert: 'true' })}
153
185
  ref={ref}
@@ -162,12 +194,13 @@ const MainSidebar = forwardRef<HTMLDivElement, MainSidebarProps>(
162
194
  type MainNavigationSidebarProps = Omit<MainSidebarProps, 'open' | 'setOpen' | 'side'>;
163
195
 
164
196
  const MainNavigationSidebar = forwardRef<HTMLDivElement, MainNavigationSidebarProps>((props, forwardedRef) => {
165
- const { navigationSidebarOpen, setNavigationSidebarOpen } = useMainContext(NAVIGATION_SIDEBAR_NAME);
197
+ const { navigationSidebarOpen, setNavigationSidebarOpen, resizing } = useMainContext(NAVIGATION_SIDEBAR_NAME);
166
198
  return (
167
199
  <MainSidebar
168
200
  {...props}
169
201
  open={navigationSidebarOpen}
170
202
  setOpen={setNavigationSidebarOpen}
203
+ resizing={resizing}
171
204
  side='inline-start'
172
205
  ref={forwardedRef}
173
206
  />
@@ -179,12 +212,14 @@ MainNavigationSidebar.displayName = NAVIGATION_SIDEBAR_NAME;
179
212
  type MainComplementarySidebarProps = Omit<MainSidebarProps, 'open' | 'setOpen' | 'side'>;
180
213
 
181
214
  const MainComplementarySidebar = forwardRef<HTMLDivElement, MainComplementarySidebarProps>((props, forwardedRef) => {
182
- const { complementarySidebarOpen, setComplementarySidebarOpen } = useMainContext(COMPLEMENTARY_SIDEBAR_NAME);
215
+ const { complementarySidebarOpen, setComplementarySidebarOpen, resizing } =
216
+ useMainContext(COMPLEMENTARY_SIDEBAR_NAME);
183
217
  return (
184
218
  <MainSidebar
185
219
  {...props}
186
220
  open={complementarySidebarOpen}
187
221
  setOpen={setComplementarySidebarOpen}
222
+ resizing={resizing}
188
223
  side='inline-end'
189
224
  ref={forwardedRef}
190
225
  />