-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
235 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import type { DTransitionState } from '../_transition'; | ||
import type { DFabButtonProps } from './FabButton'; | ||
import type { DElementSelector } from '@react-devui/hooks/useElement'; | ||
|
||
import React, { useRef, useState } from 'react'; | ||
|
||
import { useElement, useEvent, useIsomorphicLayoutEffect, useResize } from '@react-devui/hooks'; | ||
import { VerticalAlignTopOutlined } from '@react-devui/icons'; | ||
import { checkNodeExist, scrollTo } from '@react-devui/utils'; | ||
|
||
import { useComponentConfig, useLayout } from '../../hooks'; | ||
import { registerComponentMate, TTANSITION_DURING_BASE } from '../../utils'; | ||
import { DTransition } from '../_transition'; | ||
import { DFabButton } from './FabButton'; | ||
|
||
export interface DFabBacktopProps extends DFabButtonProps { | ||
dPage?: DElementSelector; | ||
dDistance?: number; | ||
dScrollBehavior?: 'instant' | 'smooth'; | ||
} | ||
|
||
const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DFab.Backtop' as const }); | ||
function FabBacktop(props: DFabBacktopProps, ref: React.ForwardedRef<HTMLButtonElement>): JSX.Element | null { | ||
const { | ||
children, | ||
dPage, | ||
dDistance = 400, | ||
dScrollBehavior = 'instant', | ||
|
||
...restProps | ||
} = useComponentConfig(COMPONENT_NAME, props); | ||
|
||
//#region Context | ||
const { dScrollEl, dResizeEl } = useLayout(); | ||
//#endregion | ||
|
||
const dataRef = useRef<{ | ||
clearTid?: () => void; | ||
}>({}); | ||
|
||
const pageEl = useElement(dPage ?? dScrollEl); | ||
const resizeEl = useElement(dResizeEl); | ||
|
||
const [visible, setVisible] = useState(false); | ||
|
||
const transitionStyles: Partial<Record<DTransitionState, React.CSSProperties>> = { | ||
enter: { opacity: 0 }, | ||
entering: { | ||
transition: ['opacity'].map((attr) => `${attr} ${TTANSITION_DURING_BASE}ms linear`).join(', '), | ||
}, | ||
leaving: { | ||
opacity: 0, | ||
transition: ['opacity'].map((attr) => `${attr} ${TTANSITION_DURING_BASE}ms linear`).join(', '), | ||
}, | ||
leaved: { display: 'none' }, | ||
}; | ||
|
||
const updateBackTop = () => { | ||
if (!pageEl) { | ||
return; | ||
} | ||
|
||
setVisible(pageEl.scrollTop >= dDistance); | ||
}; | ||
useIsomorphicLayoutEffect(() => { | ||
updateBackTop(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
useResize(resizeEl, updateBackTop); | ||
useEvent(pageEl, 'scroll', updateBackTop, { passive: true }); | ||
|
||
return ( | ||
<DTransition dIn={visible} dDuring={TTANSITION_DURING_BASE}> | ||
{(state) => ( | ||
<DFabButton | ||
{...restProps} | ||
ref={ref} | ||
style={{ | ||
...restProps.style, | ||
...transitionStyles[state], | ||
}} | ||
onClick={(e) => { | ||
restProps.onClick?.(e); | ||
|
||
if (pageEl) { | ||
dataRef.current.clearTid?.(); | ||
dataRef.current.clearTid = scrollTo(pageEl, { | ||
top: 0, | ||
behavior: dScrollBehavior, | ||
}); | ||
} | ||
}} | ||
> | ||
{checkNodeExist(children) ? children : <VerticalAlignTopOutlined />} | ||
</DFabButton> | ||
)} | ||
</DTransition> | ||
); | ||
} | ||
|
||
export const DFabBacktop = React.forwardRef(FabBacktop); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
title: | ||
en-US: Loading | ||
zh-CN: 加载中 | ||
--- | ||
|
||
# en-US | ||
|
||
Add the `dLoading` attribute to make the button in the loading state. | ||
|
||
# zh-CN | ||
|
||
添加 `dLoading` 属性即可让按钮处于加载状态。 | ||
|
||
```tsx | ||
import { useState, useRef } from 'react'; | ||
|
||
import { useAsync, useImmer } from '@react-devui/hooks'; | ||
import { CaretRightOutlined, DeleteOutlined } from '@react-devui/icons'; | ||
import { DFab } from '@react-devui/ui'; | ||
|
||
export default function Demo() { | ||
const dataRef = useRef({}); | ||
const async = useAsync(); | ||
|
||
const [loading, setLoading] = useImmer(false); | ||
const [expand, setExpand] = useImmer(false); | ||
|
||
return ( | ||
<DFab | ||
dList={[ | ||
{ | ||
placement: 'right', | ||
actions: [ | ||
<DFab.Button | ||
dTheme="danger" | ||
dVariant="circle" | ||
dLoading={loading} | ||
onClick={() => { | ||
setLoading(true); | ||
dataRef.current.clearTid?.(); | ||
dataRef.current.clearTid = async.setTimeout(() => { | ||
setLoading(false); | ||
setExpand(false); | ||
}, 1000); | ||
}} | ||
> | ||
<DeleteOutlined /> | ||
</DFab.Button>, | ||
], | ||
}, | ||
]} | ||
dExpand={expand} | ||
> | ||
<DFab.Button dVariant="circle" onClick={() => setExpand((draft) => !draft)}> | ||
<CaretRightOutlined /> | ||
</DFab.Button> | ||
</DFab> | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: | ||
en-US: Backtop | ||
zh-CN: 回到顶部 | ||
--- | ||
|
||
# en-US | ||
|
||
`DFab.Backtop` component provided. | ||
|
||
# zh-CN | ||
|
||
提供了 `DFab.Backtop` 组件。 | ||
|
||
```tsx | ||
import { DFab } from '@react-devui/ui'; | ||
|
||
export default function Demo() { | ||
return ( | ||
<DFab style={{ position: 'fixed', right: 100, bottom: 40 }}> | ||
<DFab.Backtop dVariant="circle" /> | ||
</DFab> | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './Fab'; | ||
export * from './FabButton'; | ||
export * from './FabBacktop'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.