-
Notifications
You must be signed in to change notification settings - Fork 57
docs: guides section #717
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
Open
hejsztynx
wants to merge
6
commits into
@ksienkiewicz/docs-core-functionalities
Choose a base branch
from
@ksienkiewicz/docs-guides
base: @ksienkiewicz/docs-core-functionalities
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
docs: guides section #717
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ffe2494
docs: guides section
hejsztynx 1bc1751
fix: grammar
hejsztynx a11e375
fix: stale link
hejsztynx 02eebe0
Merge branch '@ksienkiewicz/docs-core-functionalities' into @ksienkie…
hejsztynx b61801e
refactor: note order
hejsztynx 15342cf
Update docs/docs/guides/emojis.mdx
hejsztynx 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,85 @@ | ||
| --- | ||
| sidebar_position: 3 | ||
| --- | ||
|
|
||
| import InteractiveExample from '@site/src/components/InteractiveExample'; | ||
| import EmojiEditor from '@site/src/examples/EmojiEditor'; | ||
| import EmojiEditorSrc from '!!raw-loader!@site/src/examples/EmojiEditor'; | ||
|
|
||
| # Emojis | ||
|
|
||
| Mentions are powerful enough that you can build much more with them. An emoji picker is just a mention where the display text does not match the query. We'll use `:` as the indicator: the user types `:smile`, picks from a list, and the shortcode is | ||
| replaced with the emoji itself. If the mention events are new to you, start with | ||
| [Mentions](/rich-text-formatting/mentions) and the | ||
| [User and channel mentions](/guides/user-and-channel-mentions) guide; this page reuses the | ||
| same flow. | ||
|
|
||
| ## A shortcode table | ||
|
|
||
| Map each shortcode to the glyph it inserts: | ||
|
|
||
| ```tsx | ||
| const EMOJIS = [ | ||
| { shortcode: 'smile', char: '😄' }, | ||
| { shortcode: 'heart', char: '❤️' }, | ||
| { shortcode: 'fire', char: '🔥' }, | ||
| { shortcode: 'rocket', char: '🚀' }, | ||
| ]; | ||
| ``` | ||
|
|
||
| ## Wiring it up | ||
|
|
||
| Register `:` as the only indicator and filter the table with the text typed | ||
| after it. Because people habitually close the shortcode (`:smile:`), strip a | ||
| trailing colon before matching: | ||
|
|
||
| ```tsx | ||
| <EnrichedTextInput | ||
| mentionIndicators={[':']} | ||
| onStartMention={() => setOpen(true)} | ||
| onChangeMention={({ text }) => setQuery(text)} | ||
| onEndMention={() => setOpen(false)} | ||
| // ... | ||
| />; | ||
|
|
||
| const q = query.replace(/:$/, '').toLowerCase(); | ||
| const suggestions = EMOJIS.filter(e => e.shortcode.startsWith(q)); | ||
| ``` | ||
|
|
||
| When the user picks, `setMention` inserts the glyph as the mention's display | ||
| text. The `data-shortcode` makes a handy attribute if you ever need to reconstruct it: | ||
|
|
||
| ```tsx | ||
| const pick = (emoji) => { | ||
| ref.current?.setMention(':', emoji.char, { | ||
| 'data-shortcode': emoji.shortcode, | ||
| }); | ||
| }; | ||
| ``` | ||
|
|
||
| Since the emoji glyph is the whole mention, drop the usual highlight so it | ||
| reads as plain text: | ||
|
|
||
| ```tsx | ||
| const htmlStyle = { | ||
| mention: { | ||
| ':': { | ||
| color: '#232736', | ||
| backgroundColor: 'transparent', | ||
| textDecorationLine: 'none', | ||
| }, | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| ## Try it out | ||
|
|
||
| Type `:` followed by a name - `:fire`, `:heart` - then tap a suggestion. The shortcode is replaced with a single emoji that you can place anywhere in the text. | ||
|
|
||
| <InteractiveExample src={EmojiEditorSrc} component={EmojiEditor} /> | ||
|
|
||
| :::info | ||
|
|
||
| If you want to see the whole code used to build this example, you can find it by switching the tab from `Preview` to `Code`. | ||
|
|
||
| ::: |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,112 @@ | ||
| --- | ||
| sidebar_position: 2 | ||
| --- | ||
|
|
||
| import InteractiveExample from '@site/src/components/InteractiveExample'; | ||
| import MentionOnlyEditor from '@site/src/examples/MentionOnlyEditor'; | ||
| import MentionOnlyEditorSrc from '!!raw-loader!@site/src/examples/MentionOnlyEditor'; | ||
|
|
||
| # User and channel mentions | ||
|
|
||
| This guide wires the mention events into a complete picker - the flow behind a | ||
| chat composer where typing `@` suggests people and `#` suggests channels. If you | ||
| haven't met the mention API yet, read | ||
| [Mentions](/rich-text-formatting/mentions) first; here we move quickly and | ||
| assume the events and `setMention` are familiar. | ||
|
|
||
| ## The data behind a mention | ||
|
|
||
| Mentions are particularly useful if they point at something. Keep two lists around - one | ||
| for users, one for channels - each item carrying an `id` you can attach | ||
| to the finished mention: | ||
|
|
||
| ```tsx | ||
| const USERS = [ | ||
| { id: 'u1', name: 'John Doe' }, | ||
| { id: 'u2', name: 'Jane Smith' }, | ||
| { id: 'u3', name: 'Alice Johnson' }, | ||
| { id: 'u4', name: 'Bob Brown' }, | ||
| ]; | ||
|
|
||
| const CHANNELS = [ | ||
| { id: 'c1', name: 'general' }, | ||
| { id: 'c2', name: 'engineering' }, | ||
| { id: 'c3', name: 'random' }, | ||
| { id: 'c4', name: 'announcements' }, | ||
| ]; | ||
| ``` | ||
|
|
||
| ## Wiring the picker | ||
|
|
||
| Now let's register both indicators and the events callbacks: | ||
|
|
||
| ```tsx | ||
| <EnrichedTextInput | ||
| mentionIndicators={['@', '#']} | ||
| onStartMention={openPicker} // fired when '@' or '#' is typed | ||
| onChangeMention={updateQuery} // fired on every keystroke after it | ||
| onEndMention={closePicker} // fired when the mention is left | ||
| // ... | ||
| /> | ||
| ``` | ||
|
|
||
| `onStartMention` hands you the indicator, so you know whether to show people or | ||
| channels. `onChangeMention` hands you the `text` typed so far - filter your list | ||
| with it. `onEndMention` fires when the cursor leaves the mention, so you dismiss | ||
| the list. | ||
|
|
||
| When the user taps a suggestion, finish the mention with `setMention`. Pass the | ||
| same indicator that started it, the display text, and | ||
| the item's data as attributes: | ||
|
|
||
| ```tsx | ||
| const pick = (item) => { | ||
| ref.current?.setMention(indicator, `${indicator}${item.name}`, { | ||
| id: item.id, | ||
| }); | ||
| }; | ||
| ``` | ||
|
|
||
| Now let's give each indicator its own look through | ||
| `htmlStyle.mention`: | ||
|
|
||
| ```tsx | ||
| const htmlStyle = { | ||
| mention: { | ||
| '@': { color: '#2b7a4b', backgroundColor: '#d8f3e3' }, | ||
| '#': { color: '#2b5f9e', backgroundColor: '#d8e6f9' }, | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| ## Try it out | ||
|
|
||
| Type `@` to filter people or `#` to filter channels, keep typing to narrow the | ||
| list, then tap a suggestion. | ||
|
|
||
| <InteractiveExample src={MentionOnlyEditorSrc} component={MentionOnlyEditor} /> | ||
|
hejsztynx marked this conversation as resolved.
|
||
|
|
||
| :::info | ||
|
|
||
| If you want to see the whole code used to build this example, you can find it by switching the tab from `Preview` to `Code`. | ||
|
|
||
| ::: | ||
|
|
||
| :::tip | ||
|
|
||
| A mention is only active while the editor is focused - if it blurs, | ||
| `onEndMention` fires and `setMention` becomes a no-op. On native, tapping a | ||
| suggestion never steals focus, so it just works. On web it does, so the rows | ||
| call `preventDefault` on `mousedown` to keep the editor focused. The example | ||
| wraps that in a small `keepEditorFocused` helper (see the Code tab). | ||
|
|
||
| ::: | ||
|
|
||
| :::note | ||
|
|
||
| The attributes you pass to `setMention` (here `{ id }`) ride along in the HTML | ||
| and survive a round-trip through `getHTML` / `setValue`. Prefix custom keys with | ||
| `data-` if they need to outlive a sanitizer - see the note in | ||
| [Mentions](/rich-text-formatting/mentions). | ||
|
|
||
| ::: | ||
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.