Skip to content

Commit 0b289e8

Browse files
committed
feat: trigger context
1 parent 92f552c commit 0b289e8

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,64 @@
11
import type { TriggerProps } from "@arco-design/web-react";
22
import { Trigger as ArcoTrigger } from "@arco-design/web-react";
33
import type { FC } from "react";
4-
import { Fragment } from "react";
4+
import { createContext, useContext, useEffect, useMemo, useState } from "react";
55

6-
export const Trigger: FC<TriggerProps & { readonly?: boolean }> = props => {
7-
if (props.readonly) {
8-
return <Fragment>{props.children}</Fragment>;
9-
}
10-
return <ArcoTrigger {...props}>{props.children}</ArcoTrigger>;
6+
import { useMemoFn } from "../hooks/preset";
7+
8+
type ContextProps = { lock: () => void; unlock: () => void };
9+
const Context = createContext<ContextProps>({
10+
lock: () => null,
11+
unlock: () => null,
12+
});
13+
14+
export const TriggerContext: FC<TriggerProps> = props => {
15+
const { children, ...rest } = props;
16+
const context = useContext(Context);
17+
const [visible, setVisible] = useState(false);
18+
const [prevVisible, setPrevVisible] = useState(true);
19+
const popupVisible = rest.popupVisible === void 0 ? visible : rest.popupVisible;
20+
21+
const lock = useMemoFn(() => {
22+
setPrevVisible(false);
23+
});
24+
25+
const unlock = useMemoFn(() => {
26+
setPrevVisible(true);
27+
});
28+
29+
const provider = useMemo(() => ({ lock, unlock }), [lock, unlock]);
30+
31+
const onVisibleChange = useMemoFn((v: boolean) => {
32+
if (rest.onVisibleChange) {
33+
rest.onVisibleChange(v);
34+
} else {
35+
setVisible(v);
36+
}
37+
});
38+
39+
useEffect(() => {
40+
if (popupVisible) {
41+
context.lock();
42+
} else {
43+
context.unlock();
44+
}
45+
}, [popupVisible, context]);
46+
47+
return (
48+
<Context.Provider value={provider}>
49+
<ArcoTrigger
50+
{...rest}
51+
popupVisible={popupVisible}
52+
onVisibleChange={onVisibleChange}
53+
style={{
54+
...rest.style,
55+
opacity: !prevVisible ? 0 : void 0,
56+
visibility: !prevVisible ? "hidden" : void 0,
57+
transition: "opacity 0.3s",
58+
}}
59+
>
60+
{children}
61+
</ArcoTrigger>
62+
</Context.Provider>
63+
);
1164
};

packages/plugin/src/table/components/pin-toolbar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Button } from "@arco-design/web-react";
2+
import { Trigger } from "@arco-design/web-react";
23
import type { EditorKit } from "doc-editor-core";
34
import type { BlockElement } from "doc-editor-delta";
45
import { Editor, Transforms } from "doc-editor-delta";
56
import { findNodePath } from "doc-editor-utils";
67
import type { FC } from "react";
78
import { useMemo } from "react";
89

9-
import { Trigger } from "../../shared/modules/trigger";
1010
import type { TableContext } from "../hooks/use-context";
1111
import { MergeIcon } from "../icon/merge";
1212
import { SplitIcon } from "../icon/split";

packages/react/src/components/preset.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const example: BaseNode[] = [
3737
},
3838
],
3939
},
40-
{ children: [{ text: "行内元素" }], heading: { type: "h2", id: "83ddface" } },
40+
{ children: [{ text: "行内结构" }], heading: { type: "h2", id: "83ddface" } },
4141
{
4242
children: [
4343
{ text: "支持" },
@@ -68,7 +68,7 @@ export const example: BaseNode[] = [
6868
],
6969
align: "center",
7070
},
71-
{ children: [{ text: "行级元素" }], heading: { id: "4644b757", type: "h2" } },
71+
{ children: [{ text: "段落结构" }], heading: { id: "4644b757", type: "h2" } },
7272
{ children: [{ text: "标题" }], heading: { type: "h3", id: "213e6703" } },
7373
{
7474
children: [
@@ -93,7 +93,7 @@ export const example: BaseNode[] = [
9393
{ "line-height": 2, "children": [{ text: "支持行级别的文字对齐。" }], "align": "left" },
9494
{
9595
"line-height": 2,
96-
"children": [{ text: "嵌套元素" }],
96+
"children": [{ text: "组合结构" }],
9797
"heading": { type: "h2", id: "QFwoE5fK" },
9898
},
9999
{ children: [{ text: "引用块" }], heading: { type: "h3", id: "8426a51b" } },
@@ -169,7 +169,7 @@ export const example: BaseNode[] = [
169169
},
170170
],
171171
},
172-
{ children: [{ text: "嵌入元素" }], heading: { type: "h2", id: "cfSqPoYb" } },
172+
{ children: [{ text: "嵌入结构" }], heading: { type: "h2", id: "cfSqPoYb" } },
173173
{ children: [{ text: "图片" }], heading: { type: "h3", id: "aab55qq1" } },
174174
{ children: [{ text: "支持图片上传。" }] },
175175
{
@@ -197,7 +197,7 @@ export const example: BaseNode[] = [
197197
],
198198
},
199199
{ "dividing-line": true, "children": [{ text: "" }] },
200-
{ children: [{ text: "块级元素" }], heading: { type: "h2", id: "DEnfO0kv" } },
200+
{ children: [{ text: "块级结构" }], heading: { type: "h2", id: "DEnfO0kv" } },
201201
{ children: [{ text: "高亮块" }], heading: { type: "h3", id: "af869b51" } },
202202
{
203203
"highlight-block": { border: "var(--arcoblue-6)", background: "var(--arcoblue-3)" },

0 commit comments

Comments
 (0)