-
Notifications
You must be signed in to change notification settings - Fork 709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add 'Keys' variable to enum values and check for hotKeys #2463
Merged
DaiQiangReal
merged 15 commits into
DouyinFE:release
from
Nathon2Y:feat/KeyboardShortCut
Sep 10, 2024
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cbc68b1
feat: add 'Keys' variable to enum values and check for hotKeys
10b6863
fix: the order of check for hotKeys
bfd97f2
feat: change the `Keys` to enum
9a30274
chore: change the judge function based on Keys enum
9c5c0e4
feat: add rest attributes
2afe495
chore: remove useless code
167e39a
style:change hotkeys spliter style
7164611
feat: add shotscreen for overview
2bc0901
feat: update APIs and doc
a6aaf94
feat: modify preventDefault api and update doc
3221829
fix: export `keys` error
31ffce1
chore: remove useless null check
f1d1901
fix: withdraw change in overlay-bg
3baa28b
feat: using optional chaining
68320fe
Merge remote-tracking branch 'semi/release' into feat/KeyboardShortCut
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ localeCode: en-US | |
order: 30 | ||
category: Input | ||
title: HotKeys | ||
icon: doc-input | ||
icon: doc-configprovider | ||
width: 60% | ||
brief: used to facilitate the customization of keyboard shortcut | ||
--- | ||
|
@@ -12,6 +12,7 @@ brief: used to facilitate the customization of keyboard shortcut | |
## Demos | ||
|
||
### How to import | ||
PinCode supported from 2.66.0 | ||
|
||
```jsx import | ||
import { HotKeys } from '@douyinfe/semi-ui'; | ||
|
@@ -20,29 +21,53 @@ import { HotKeys } from '@douyinfe/semi-ui'; | |
### Explaination | ||
The hotkeys only support combinations of modifier keys like Shift, Control, Meta, and Alt with other keys. | ||
|
||
When a hotkey is set to a common shortcut like Ctrl/Meta + C, it may prevent the default behavior (e.g., copying) from being triggered properly. | ||
> [Meta](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/metaKey) corresponds to Command on macOS and Win on Windows. | ||
|
||
When setting a shortcut that overlaps with common shortcuts like Ctrl/Meta + C, the blockDefault setting can be used to control whether the default event is triggered. | ||
|
||
|
||
|
||
### Basic Usage | ||
|
||
Pass in key combinations via `hotKeys` and bind a shortcut handler function using `onClick` to respond to the action. | ||
Pass in key combinations via `hotKeys` and bind a shortcut handler function using `onHotKey` to respond to the action. | ||
|
||
When pressing `Ctrl + Shift + A`, it increments the count by 1. By default, it listens on the body, making it effective globally. | ||
When pressing `Ctrl + Shift + A`, it opens the modal. By default, it listens on the body, making it effective globally. | ||
|
||
[values reference](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values) | ||
|
||
It's also recommended to use the `Keys` wrapper to set hotkeys. | ||
```jsx import | ||
import { Keys } from '@douyinfe/semi-ui'; | ||
``` | ||
|
||
```jsx live=true | ||
import React, { useState } from 'react'; | ||
import { HotKeys } from '@douyinfe/semi-ui'; | ||
import { HotKeys, Keys, Modal } from '@douyinfe/semi-ui'; | ||
|
||
function Demo() { | ||
const [cnt, setCnt] = useState(0) | ||
const onClick = () => { | ||
setCnt(cnt+1) | ||
} | ||
const [visible, setVisible] = useState(false); | ||
const showDialog = () => { | ||
setVisible(true); | ||
}; | ||
const handleOk = () => { | ||
setVisible(false); | ||
}; | ||
const handleCancel = () => { | ||
setVisible(false); | ||
}; | ||
const hotKeys = [Keys.Control, 'Shift', Keys.A] | ||
|
||
return ( | ||
<div> | ||
<HotKeys hotKeys={['Control', 'Shift', 'a']} onClick={onClick} ></HotKeys> | ||
<div>{cnt}</div> | ||
<HotKeys hotKeys={hotKeys} onHotKey={showDialog} ></HotKeys> | ||
<Modal | ||
title="Dialog" | ||
visible={visible} | ||
onOk={handleOk} | ||
onCancel={handleCancel} | ||
> | ||
This is the Modal opened by hotkey: {hotKeys.join('+')}. | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
|
@@ -54,71 +79,106 @@ Set the characters through `content` | |
|
||
```jsx live=true | ||
import React, { useState } from 'react'; | ||
import { HotKeys } from '@douyinfe/semi-ui'; | ||
import { HotKeys, Keys, Modal } from '@douyinfe/semi-ui'; | ||
|
||
function Demo() { | ||
const [cnt, setCnt] = useState(0) | ||
const onClick = () => { | ||
setCnt(cnt+1) | ||
} | ||
const [visible, setVisible] = useState(false); | ||
const showDialog = () => { | ||
setVisible(true); | ||
}; | ||
const handleOk = () => { | ||
setVisible(false); | ||
}; | ||
const handleCancel = () => { | ||
setVisible(false); | ||
}; | ||
const hotKeys = [Keys.Control, 'Shift', Keys.B] | ||
|
||
return ( | ||
<div> | ||
<HotKeys hotKeys={["Control", "b"]} onClick={onClick} content={["Ctrl", "B"]}></HotKeys> | ||
<br></br> | ||
<HotKeys hotKeys={["Meta","b"]} onClick={onClick} content={["⌘", "B"]}></HotKeys> | ||
<div>{cnt}</div> | ||
<HotKeys hotKeys={hotKeys} onHotKey={showDialog} content={['Ctrl', 'Shift', 'B']}></HotKeys> | ||
<Modal | ||
title="Dialog" | ||
visible={visible} | ||
onOk={handleOk} | ||
onCancel={handleCancel} | ||
> | ||
This is the Modal opened by hotkey: {hotKeys.join('+')}. | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
Replace the element through `render` | ||
|
||
When encountering issues caused by different operating system shortcuts, you can similarly use two components and customize the rendering. | ||
```jsx live=true | ||
import React, { useState } from 'react'; | ||
import { HotKeys, Tag } from '@douyinfe/semi-ui'; | ||
import { HotKeys, Keys, Modal, Tag } from '@douyinfe/semi-ui'; | ||
|
||
function Demo() { | ||
const hotKeys = ["r"] | ||
const [cnt, setCnt] = useState(0) | ||
const onClick = () => { | ||
setCnt(cnt+1) | ||
} | ||
const newShortCut = () => { | ||
return ( | ||
<div> | ||
<Tag>{"Click R / K to add"}</Tag> | ||
</div> | ||
) | ||
} | ||
const [visible, setVisible] = useState(false); | ||
const showDialog = () => { | ||
setVisible(true); | ||
}; | ||
const handleOk = () => { | ||
setVisible(false); | ||
}; | ||
const handleCancel = () => { | ||
setVisible(false); | ||
}; | ||
const hotKeys = [Keys.Control, Keys.R] | ||
|
||
const newHotKeys = <Tag>Press Ctrl+R to Open Modal</Tag> | ||
return ( | ||
<div> | ||
<HotKeys hotKeys={hotKeys} onClick={onClick} render={newShortCut}></HotKeys> | ||
<HotKeys hotKeys={["k"]} onClick={onClick} render={() => null}></HotKeys> | ||
<div>{cnt}</div> | ||
<HotKeys hotKeys={hotKeys} onHotKey={showDialog} render={newHotKeys}></HotKeys> | ||
<Modal | ||
title="Dialog" | ||
visible={visible} | ||
onOk={handleOk} | ||
onCancel={handleCancel} | ||
> | ||
This is the Modal opened by hotkey: {hotKeys.join('+')}. | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
### Clickable | ||
### prevent Default event | ||
|
||
Trigger the function by clicking component | ||
Control the default event by setting `blockDefault`. | ||
```jsx live=true | ||
import React, { useState } from 'react'; | ||
import { HotKeys } from '@douyinfe/semi-ui'; | ||
import { HotKeys, Keys, Modal } from '@douyinfe/semi-ui'; | ||
|
||
function Demo() { | ||
const hotKeys = ["Control", "a"] | ||
const [cnt, setCnt] = useState(0) | ||
const onClick = () => { | ||
setCnt(cnt+1) | ||
} | ||
const [visible, setVisible] = useState(false); | ||
const showDialog = () => { | ||
setVisible(true); | ||
}; | ||
const handleOk = () => { | ||
setVisible(false); | ||
}; | ||
const handleCancel = () => { | ||
setVisible(false); | ||
}; | ||
const hotKeys = [Keys.Meta, Keys.S] | ||
|
||
return ( | ||
<div> | ||
<HotKeys hotKeys={hotKeys} onClick={onClick} clickable></HotKeys> | ||
<div>{cnt}</div> | ||
<HotKeys hotKeys={hotKeys} onHotKey={showDialog} blockDefault></HotKeys> | ||
<br /> | ||
<HotKeys hotKeys={[Keys.Control, Keys.S]} onHotKey={showDialog} blockDefault></HotKeys> | ||
<Modal | ||
title="Dialog" | ||
visible={visible} | ||
onOk={handleOk} | ||
onCancel={handleCancel} | ||
> | ||
This is the Modal opened by hotkey: {'Meta/Control + S'}. | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
|
@@ -128,46 +188,36 @@ function Demo() { | |
The hotkey is listened to on the body by default, through `getListenerTarget` | ||
```jsx live=true | ||
import React, { useState, useRef } from 'react'; | ||
import { HotKeys, Input } from '@douyinfe/semi-ui'; | ||
import { HotKeys, Input, Modal } from '@douyinfe/semi-ui'; | ||
|
||
function Demo() { | ||
const hotKeys = ["Meta", "s"] | ||
const [cnt, setCnt] = useState(0) | ||
const onClick = () => { | ||
setCnt(cnt+1) | ||
} | ||
const hotKeys = ["Control", "q"] | ||
const [visible, setVisible] = useState(false); | ||
const showDialog = () => { | ||
setVisible(true); | ||
}; | ||
const handleOk = () => { | ||
setVisible(false); | ||
}; | ||
const handleCancel = () => { | ||
setVisible(false); | ||
}; | ||
|
||
const inputRef = useRef(null); | ||
return ( | ||
<div> | ||
<Input ref={inputRef} placeholder='test for target'></Input> | ||
<HotKeys hotKeys={hotKeys} onClick={onClick} | ||
<HotKeys hotKeys={hotKeys} onHotKey={showDialog} | ||
getListenerTarget={() => inputRef.current}> | ||
</HotKeys> | ||
<div>{cnt}</div> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
### Disabled | ||
By setting `disabled` as `true`, the component will not listen hotkeys. | ||
You can use it when only styling is needed. | ||
|
||
```jsx live=true | ||
import React, { useState } from 'react'; | ||
import { HotKeys } from '@douyinfe/semi-ui'; | ||
|
||
function Demo() { | ||
const hotKeys = ["Control", "a"] | ||
const [cnt, setCnt] = useState(0) | ||
const onClick = () => { | ||
setCnt(cnt+1) | ||
} | ||
return ( | ||
<div> | ||
<HotKeys hotKeys={hotKeys} onClick={onClick} disabled></HotKeys> | ||
<div>{cnt}</div> | ||
<Modal | ||
title="Dialog" | ||
visible={visible} | ||
onOk={handleOk} | ||
onCancel={handleCancel} | ||
> | ||
This is the Modal opened by hotkey: {hotKeys.join('+')}. | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
|
@@ -180,14 +230,14 @@ function Demo() { | |
|
||
| Property | Instructions | type | Default | | ||
|-------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------|-----------| | ||
| hotKeys | Define keyboard shortcut,[values](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values) | KeyboardEvent.key[] | - | | ||
| blockDefault | Whether to prevent the default behavior of the shortcut | boolean | false | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改成和原生一样的 preventDefault 吧 |
||
| className | class name | string | | | ||
| content | Set the characters | string[] | - | | ||
| onClick | function that keyboard shortcut triggers | () => void | - | | ||
| clickable | whether the function can be triggered by click | boolean | false | | ||
| render | Replace the element | () => ReactNode \| ReactNode | | | ||
| className | class name | string | | | ||
| getListenerTarget | change the DOM element the listener is mounted on | () => HTMLElement | document.body | | ||
| disabled | | boolean | false | | ||
| hotKeys | Define keyboard shortcut,[values](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values) | KeyboardEvent.key[] | - | | ||
| onClick | callback that clicking triggers | () => void | - | | ||
| onHotKey | callback that hotKeys triggers | () => void | - | | ||
| render | Replace the element | () => ReactNode \| ReactNode | | | ||
| style | style | CSSProperties | | | ||
|
||
## Design Tokens | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
把枚举的 Keys 放到 HotKeys 的static 属性上吧,不要让用户单独 import 了,有点分散