Skip to content

Commit

Permalink
feat(NavBar): 新增回调Rect相关数据方法
Browse files Browse the repository at this point in the history
  • Loading branch information
79E committed Dec 2, 2022
1 parent 0bcb8be commit 4901fdc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/components/nav-bar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ import { NavBar } from 'aunt';
| ------------ | ------------ | -------------------------------- | ------ |
| onClickLeft | 点击左侧按钮 | `(e: React.MouseEvent) => void;` | `-` |
| onClickRight | 点击右侧按钮 | `(e: React.MouseEvent) => void;` | `-` |
| callbackRect | 回调返回导航的相关数据 | `(rect: Rect) => void` | `-` |

```ts
type RectKey = "top" | "left" | "right" | "bottom" | "width" | "height";
export type Rect = {
[key in RectKey]: number
}
```
## 样式变量
Expand Down
8 changes: 6 additions & 2 deletions src/components/nav-bar/nav-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const NavBar: FunctionComponent<NavBarProps> = props => {

const navBarRef = useRef(null);

const navBarHeight = getRect(navBarRef).height;
const navBarRect = getRect(navBarRef);

const renderLeft = () => {
return (
Expand Down Expand Up @@ -50,7 +50,7 @@ export const NavBar: FunctionComponent<NavBarProps> = props => {
return (
<div
className={ns.e('placeholder')}
style={{ height: navBarHeight ? `${navBarHeight}px` : undefined }}
style={{ height: navBarRect.height ? `${navBarRect.height}px` : undefined }}
/>
);
}
Expand Down Expand Up @@ -79,6 +79,10 @@ export const NavBar: FunctionComponent<NavBarProps> = props => {
};
}, [props.style, props.statusBarHeight]);

React.useLayoutEffect(() => {
props.callbackRect?.(navBarRect);
}, [navBarRect]);

return (
<>
{renderPlaceholder()}
Expand Down
8 changes: 7 additions & 1 deletion src/components/nav-bar/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { BaseTypeProps } from '../../utils';
import type { BaseTypeProps } from '../../utils';
import type { Rect } from '../../utils/dom/getRect';

export interface NavBarProps extends BaseTypeProps {
/**
Expand Down Expand Up @@ -36,4 +37,9 @@ export interface NavBarProps extends BaseTypeProps {
placeholder?: boolean;
onClickLeft?: (e: React.MouseEvent) => void;
onClickRight?: (e: React.MouseEvent) => void;
/**
* 回调导航栏的相关数据
* @param rect ("top" | "left" | "right" | "bottom" | "width" | "height")
*/
callbackRect?: (rect: Rect) => void;
}
14 changes: 4 additions & 10 deletions src/utils/dom/getRect.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { useEffect, useState } from 'react';
import { useRect } from '../../hooks';

type Key = 'top' | 'left' | 'right' | 'bottom' | 'width' | 'height';

interface Rect {
top: number;
left: number;
right: number;
bottom: number;
width: number;
height: number;
}
type RectKey = 'top' | 'left' | 'right' | 'bottom' | 'width' | 'height';
export type Rect = {
[key in RectKey]: number;
};

export const getRect = (element: { current: Element | undefined | null }): Rect => {
const [rect, setRect] = useState({
Expand Down

0 comments on commit 4901fdc

Please sign in to comment.