mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
fix: remove shake in first render (#2190)
This commit is contained in:
@@ -75,7 +75,7 @@ export const RootAppSidebar = ({
|
|||||||
}, [createPage, openPage]);
|
}, [createPage, openPage]);
|
||||||
const sidebarOpen = useAtomValue(appSidebarOpenAtom);
|
const sidebarOpen = useAtomValue(appSidebarOpenAtom);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (environment.isDesktop) {
|
if (environment.isDesktop && typeof sidebarOpen === 'boolean') {
|
||||||
window.apis?.onSidebarVisibilityChange(sidebarOpen);
|
window.apis?.onSidebarVisibilityChange(sidebarOpen);
|
||||||
}
|
}
|
||||||
}, [sidebarOpen]);
|
}, [sidebarOpen]);
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import { atomWithStorage } from 'jotai/utils';
|
import { atomWithStorage } from 'jotai/utils';
|
||||||
|
|
||||||
export const appSidebarOpenAtom = atomWithStorage('app-sidebar-open', true);
|
export const appSidebarOpenAtom = atomWithStorage(
|
||||||
|
'app-sidebar-open',
|
||||||
|
undefined as boolean | undefined
|
||||||
|
);
|
||||||
export const appSidebarWidthAtom = atomWithStorage(
|
export const appSidebarWidthAtom = atomWithStorage(
|
||||||
'app-sidebar-width',
|
'app-sidebar-width',
|
||||||
256 /* px */
|
256 /* px */
|
||||||
|
|||||||
@@ -8,7 +8,13 @@ import { assignInlineVars } from '@vanilla-extract/dynamic';
|
|||||||
import { useAtom, useAtomValue } from 'jotai';
|
import { useAtom, useAtomValue } from 'jotai';
|
||||||
import type { PropsWithChildren, ReactElement } from 'react';
|
import type { PropsWithChildren, ReactElement } from 'react';
|
||||||
import type { ReactNode } from 'react';
|
import type { ReactNode } from 'react';
|
||||||
import { forwardRef, useCallback, useImperativeHandle, useRef } from 'react';
|
import {
|
||||||
|
forwardRef,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useImperativeHandle,
|
||||||
|
useRef,
|
||||||
|
} from 'react';
|
||||||
|
|
||||||
import { IconButton } from '../../ui/button/IconButton';
|
import { IconButton } from '../../ui/button/IconButton';
|
||||||
import {
|
import {
|
||||||
@@ -34,6 +40,7 @@ export const AppSidebar = forwardRef<HTMLElement, AppSidebarProps>(
|
|||||||
const [open, setOpen] = useAtom(appSidebarOpenAtom);
|
const [open, setOpen] = useAtom(appSidebarOpenAtom);
|
||||||
|
|
||||||
const appSidebarWidth = useAtomValue(appSidebarWidthAtom);
|
const appSidebarWidth = useAtomValue(appSidebarWidthAtom);
|
||||||
|
const initialRender = open === undefined;
|
||||||
|
|
||||||
const handleSidebarOpen = useCallback(() => {
|
const handleSidebarOpen = useCallback(() => {
|
||||||
setOpen(open => !open);
|
setOpen(open => !open);
|
||||||
@@ -41,8 +48,20 @@ export const AppSidebar = forwardRef<HTMLElement, AppSidebarProps>(
|
|||||||
|
|
||||||
useImperativeHandle(forwardedRef, () => ref.current as HTMLElement);
|
useImperativeHandle(forwardedRef, () => ref.current as HTMLElement);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (open === undefined && ref.current) {
|
||||||
|
const initialOpen =
|
||||||
|
window.getComputedStyle(ref.current).position === 'relative';
|
||||||
|
|
||||||
|
setOpen(initialOpen);
|
||||||
|
}
|
||||||
|
}, [open, setOpen]);
|
||||||
|
|
||||||
const environment = getEnvironment();
|
const environment = getEnvironment();
|
||||||
const isMacosDesktop = environment.isDesktop && environment.isMacOs;
|
const isMacosDesktop = environment.isDesktop && environment.isMacOs;
|
||||||
|
if (initialRender) {
|
||||||
|
return <div />;
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<nav
|
<nav
|
||||||
@@ -96,9 +115,7 @@ export const AppSidebar = forwardRef<HTMLElement, AppSidebarProps>(
|
|||||||
data-testid="app-sidebar-float-mask"
|
data-testid="app-sidebar-float-mask"
|
||||||
data-open={open}
|
data-open={open}
|
||||||
className={sidebarFloatMaskStyle}
|
className={sidebarFloatMaskStyle}
|
||||||
onClick={useCallback(() => {
|
onClick={() => setOpen(false)}
|
||||||
setOpen(false);
|
|
||||||
}, [setOpen])}
|
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user