Skip to content

Commit

Permalink
remove some timer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyxb committed Sep 26, 2024
1 parent 41e5269 commit 4d70f95
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ export const root = style({
width: panelWidthVar,
minWidth: panelWidthVar,
height: '100%',
zIndex: 4,
transform: 'translateX(0)',
maxWidth: '50%',
selectors: {
'&[data-is-floating="true"]': {
position: 'absolute',
width: `calc(${panelWidthVar})`,
zIndex: 4,
},
'&[data-open="true"]': {
maxWidth: '50%',
},
'&[data-open="false"][data-handle-position="right"]': {
marginLeft: `calc(${panelWidthVar} * -1)`,
},
'&[data-open="false"][data-handle-position="left"]': {
marginRight: `calc(${panelWidthVar} * -1)`,
},
'&[data-enable-animation="true"][data-is-floating="false"]': {
transition: `margin-left ${animationTimeout} .05s, margin-right ${animationTimeout} .05s, width ${animationTimeout} .05s,background ${animationTimeout} .05s,scale ${animationTimeout} .05s`,
},
'&[data-enable-animation="true"][data-is-floating="true"]': {
transition: 'margin-left 0.5s cubic-bezier(0.22,1,0.36,1)',
'&[data-open="false"][data-handle-position="right"],&[data-is-floating="true"][data-handle-position="right"]':
{
marginLeft: `calc(${panelWidthVar} * -1)`,
},
'&[data-open="false"][data-handle-position="left"],&[data-is-floating="true"][data-handle-position="left"]':
{
marginRight: `calc(${panelWidthVar} * -1)`,
},
'&[data-open="true"][data-handle-position="right"][data-is-floating="true"]':
{
transform: `translateX(${panelWidthVar})`,
},
'&[data-open="true"][data-handle-position="left"][data-is-floating="true"]':
{
transform: `translateX(-${panelWidthVar})`,
},
'&[data-enable-animation="true"]': {
transition: `margin-left ${animationTimeout}, margin-right ${animationTimeout}, transform ${animationTimeout}, background ${animationTimeout}`,
},
'&[data-transition-state="exited"]': {
// avoid focus on hidden panel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { assignInlineVars } from '@vanilla-extract/dynamic';
import clsx from 'clsx';
import {
forwardRef,
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from 'react';
import { forwardRef, useCallback, useLayoutEffect, useRef } from 'react';
import { useTransition } from 'react-transition-state';

import * as styles from './resize-panel.css';
Expand Down Expand Up @@ -129,17 +122,6 @@ const ResizeHandle = ({
);
};

// delay initial animation to avoid flickering
function useEnableAnimation() {
const [enable, setEnable] = useState(false);
useEffect(() => {
window.setTimeout(() => {
setEnable(true);
}, 500);
}, []);
return enable;
}

const animationTimeout = 300;

export const ResizePanel = forwardRef<HTMLDivElement, ResizePanelProps>(
Expand All @@ -152,7 +134,7 @@ export const ResizePanel = forwardRef<HTMLDivElement, ResizePanelProps>(
maxWidth,
width,
floating,
enableAnimation: _enableAnimation = true,
enableAnimation = true,
open,
unmountOnExit,
onOpen,
Expand All @@ -165,7 +147,6 @@ export const ResizePanel = forwardRef<HTMLDivElement, ResizePanelProps>(
},
ref
) {
const enableAnimation = useEnableAnimation() && _enableAnimation;
const safeWidth = Math.min(maxWidth, Math.max(minWidth, width));
const [{ status }, toggle] = useTransition({
timeout: animationTimeout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const DesktopLayout = ({ children }: PropsWithChildren) => {
<AppTabsHeader
left={
<>
<SidebarSwitch show enableOpenHoverSidebar />
<SidebarSwitch show />
<NavigationButtons />
</>
}
Expand Down
9 changes: 1 addition & 8 deletions packages/frontend/core/src/components/pure/header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,8 @@ interface HeaderPros {
export const Header = ({ left, center, right }: HeaderPros) => {
const appSidebarService = useService(AppSidebarService).sidebar;
const open = useLiveData(appSidebarService.open$);
const appSidebarFloating = useLiveData(appSidebarService.responsiveFloating$);
const hoverFloating = useLiveData(appSidebarService.hoverFloating$);
return (
<div
className={clsx(style.header)}
data-open={open}
data-sidebar-floating={appSidebarFloating || hoverFloating}
data-testid="header"
>
<div className={clsx(style.header)} data-open={open} data-testid="header">
<div className={clsx(style.headerSideContainer)}>
<div className={clsx(style.headerItem, 'left')}>
<div>{left}</div>
Expand Down
22 changes: 1 addition & 21 deletions packages/frontend/core/src/components/workspace/index.css.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cssVar, lightCssVariables } from '@toeverything/theme';
import { createVar, globalStyle, keyframes, style } from '@vanilla-extract/css';
import { createVar, globalStyle, style } from '@vanilla-extract/css';

export const panelWidthVar = createVar('panel-width');

Expand Down Expand Up @@ -42,19 +42,7 @@ globalStyle(`html[data-theme="dark"] ${appStyle}`, {
},
});

const anime = keyframes({
'0%': {
marginLeft: '8px',
},
'100%': {
marginLeft: '0',
},
});

export const mainContainerStyle = style({
vars: {
[panelWidthVar]: '256px',
},
position: 'relative',
zIndex: 0,
width: '100%',
Expand All @@ -76,14 +64,6 @@ export const mainContainerStyle = style({
},
},
},
'&[data-side-bar-open="true"][data-show-pin-sidebar-animation="true"]': {
marginLeft: panelWidthVar,
transition: 'margin-left 0.5s ease-in-out',
},
'&[data-client-border="true"][data-side-bar-open="true"][data-side-bar-floating="false"]':
{
animation: `${anime} 0.5s ease-in-out forwards`,
},
'&[data-client-border="true"][data-is-desktop="true"]': {
marginTop: 0,
},
Expand Down
38 changes: 3 additions & 35 deletions packages/frontend/core/src/components/workspace/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { useAppSettingHelper } from '@affine/core/components/hooks/affine/use-app-setting-helper';
import { AppSidebarService } from '@affine/core/modules/app-sidebar';
import {
DocsService,
GlobalContextService,
useLiveData,
useService,
} from '@toeverything/infra';
import { assignInlineVars } from '@vanilla-extract/dynamic';
import { clsx } from 'clsx';
import type { HTMLAttributes, PropsWithChildren, ReactElement } from 'react';
import { forwardRef, useMemo } from 'react';
import { forwardRef } from 'react';

import {
appStyle,
mainContainerStyle,
panelWidthVar,
toolStyle,
} from './index.css';
import { appStyle, mainContainerStyle, toolStyle } from './index.css';

export type WorkspaceRootProps = PropsWithChildren<{
className?: string;
Expand Down Expand Up @@ -54,41 +47,16 @@ export interface MainContainerProps extends HTMLAttributes<HTMLDivElement> {}
export const MainContainer = forwardRef<
HTMLDivElement,
PropsWithChildren<MainContainerProps>
>(function MainContainer(
{ className, children, style, ...props },
ref
): ReactElement {
const appSidebarService = useService(AppSidebarService).sidebar;
const appSideBarOpen = useLiveData(appSidebarService.open$);
const appSidebarHoverFloating = useLiveData(appSidebarService.hoverFloating$);
const appSideBarWidth = useLiveData(appSidebarService.width$);

const showAppSideBarPinAnimation = useLiveData(
appSidebarService.showFloatToPinAnimation$
);
>(function MainContainer({ className, children, ...props }, ref): ReactElement {
const { appSettings } = useAppSettingHelper();

const combinedStyle = useMemo(() => {
const dynamicStyle = assignInlineVars({
[panelWidthVar]: `${appSideBarWidth}px`,
});
return {
...style,
...dynamicStyle,
};
}, [appSideBarWidth, style]);

return (
<div
{...props}
style={combinedStyle}
className={clsx(mainContainerStyle, className)}
data-is-desktop={BUILD_CONFIG.isElectron}
data-transparent={false}
data-client-border={appSettings.clientBorder}
data-side-bar-open={appSideBarOpen}
data-side-bar-floating={appSidebarHoverFloating}
data-show-pin-sidebar-animation={showAppSideBarPinAnimation}
data-testid="main-container"
ref={ref}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { map } from 'rxjs';

import type { AppSidebarState } from '../providers/storage';

const isMobile = !BUILD_CONFIG.isElectron && window.innerWidth < 768;

enum APP_SIDEBAR_STATE {
OPEN = 'open',
WIDTH = 'width',
Expand All @@ -15,11 +13,15 @@ export class AppSidebar extends Entity {
super();
}

/**
* whether the sidebar is open,
* even if the sidebar is not open, hovering can show the floating sidebar
*/
open$ = LiveData.from(
this.appSidebarState
.watch<boolean>(APP_SIDEBAR_STATE.OPEN)
.pipe(map(value => value ?? !isMobile)),
!isMobile
.pipe(map(value => value ?? true)),
true
);

width$ = LiveData.from(
Expand All @@ -28,11 +30,17 @@ export class AppSidebar extends Entity {
.pipe(map(value => value ?? 248)),
248
);
responsiveFloating$ = new LiveData<boolean>(isMobile);
hoverFloating$ = new LiveData<boolean>(false);
resizing$ = new LiveData<boolean>(false);

showFloatToPinAnimation$ = new LiveData<boolean>(false);
/**
* hovering can show the floating sidebar, without open it
*/
hovering$ = new LiveData<boolean>(false);

/**
* small screen mode, will disable hover effect
*/
smallScreenMode$ = new LiveData<boolean>(false);
resizing$ = new LiveData<boolean>(false);

getCachedAppSidebarOpenState = () => {
return this.appSidebarState.get<boolean>(APP_SIDEBAR_STATE.OPEN);
Expand All @@ -44,26 +52,15 @@ export class AppSidebar extends Entity {

setOpen = (open: boolean) => {
this.appSidebarState.set(APP_SIDEBAR_STATE.OPEN, open);
if (!open && this.hoverFloating$.value) {
const timeout = setTimeout(() => {
this.setHoverFloating(false);
}, 500);
return () => {
clearTimeout(timeout);
};
}
return;
};

setResponsiveFloating = (floating: boolean) => {
this.responsiveFloating$.next(floating);
setSmallScreenMode = (smallScreenMode: boolean) => {
this.smallScreenMode$.next(smallScreenMode);
};

setHoverFloating = (hoverFloating: boolean) => {
if (hoverFloating) {
this.showFloatToPinAnimation$.next(false);
}
this.hoverFloating$.next(hoverFloating);
setHovering = (hoverFloating: boolean) => {
this.hovering$.next(hoverFloating);
};

setResizing = (resizing: boolean) => {
Expand All @@ -73,8 +70,4 @@ export class AppSidebar extends Entity {
setWidth = (width: number) => {
this.appSidebarState.set(APP_SIDEBAR_STATE.WIDTH, width);
};

setShowFloatToPinAnimation = (show: boolean) => {
this.showFloatToPinAnimation$.next(show);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ export const hoverNavWrapperStyle = style({
boxShadow: cssVar('--affine-popover-shadow'),
borderRadius: '6px',
},
'&[data-is-floating="true"][data-show-pin-animation="true"]': {
marginLeft: '0',
},
'&[data-is-floating="true"][data-is-electron="true"]': {
height: '100%',
marginTop: '-4px',
Expand Down
Loading

0 comments on commit 4d70f95

Please sign in to comment.