@portabletext/plugin-emoji-picker 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Proposed, but not required, function to create a `MatchEmojis` function.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * const matchEmojis = createMatchEmojis({
7
+ * emojis: {
8
+ * '😂': ['joy'],
9
+ * '😹': ['joy_cat'],
10
+ * },
11
+ * })
12
+ * ```
13
+ *
14
+ * @beta
15
+ */
16
+ export declare function createMatchEmojis(config: {
17
+ emojis: Record<string, ReadonlyArray<string>>
18
+ }): MatchEmojis
19
+
20
+ /**
21
+ * Proposed, but not required type, to represent an emoji match.
22
+ *
23
+ * @example
24
+ * ```tsx
25
+ * {
26
+ * type: 'exact',
27
+ * key: '😂-joy',
28
+ * emoji: '😂',
29
+ * keyword: 'joy',
30
+ * }
31
+ * ```
32
+ * @example
33
+ * ```tsx
34
+ * {
35
+ * type: 'partial',
36
+ * key: '😹-joy-_cat',
37
+ * emoji: '😹',
38
+ * keyword: 'joy',
39
+ * startSlice: '',
40
+ * endSlice: '_cat',
41
+ * }
42
+ * ```
43
+ *
44
+ * @beta
45
+ */
46
+ export declare type EmojiMatch =
47
+ | {
48
+ type: 'exact'
49
+ key: string
50
+ emoji: string
51
+ keyword: string
52
+ }
53
+ | {
54
+ type: 'partial'
55
+ key: string
56
+ emoji: string
57
+ keyword: string
58
+ startSlice: string
59
+ endSlice: string
60
+ }
61
+
62
+ /**
63
+ * @beta
64
+ */
65
+ export declare type EmojiPicker<TEmojiMatch = EmojiMatch> = {
66
+ /**
67
+ * The matched keyword, including colons.
68
+ *
69
+ * Can be used to display the keyword in the UI or conditionally render the
70
+ * list of matches.
71
+ *
72
+ * @example
73
+ * ```tsx
74
+ * if (keyword.length < 2) {
75
+ * return null
76
+ * }
77
+ * ```
78
+ */
79
+ keyword: string
80
+ /**
81
+ * Emoji matches found for the current keyword.
82
+ *
83
+ * Can be used to display the matches in a list.
84
+ */
85
+ matches: ReadonlyArray<TEmojiMatch>
86
+ /**
87
+ * The index of the selected match.
88
+ *
89
+ * Can be used to highlight the selected match in the list.
90
+ *
91
+ * @example
92
+ * ```tsx
93
+ * <EmojiListItem
94
+ * key={match.key}
95
+ * match={match}
96
+ * selected={selectedIndex === index}
97
+ * />
98
+ * ```
99
+ */
100
+ selectedIndex: number
101
+ /**
102
+ * Navigate to a specific match by index.
103
+ *
104
+ * Can be used to control the `selectedIndex`. For example, using
105
+ * `onMouseEnter`.
106
+ *
107
+ * @example
108
+ * ```tsx
109
+ * <EmojiListItem
110
+ * key={match.key}
111
+ * match={match}
112
+ * selected={selectedIndex === index}
113
+ * onMouseEnter={() => {onNavigateTo(index)}}
114
+ * />
115
+ * ```
116
+ */
117
+ onNavigateTo: (index: number) => void
118
+ /**
119
+ * Select the current match.
120
+ *
121
+ * Can be used to insert the currently selected match.
122
+ *
123
+ *
124
+ * @example
125
+ * ```tsx
126
+ * <EmojiListItem
127
+ * key={match.key}
128
+ * match={match}
129
+ * selected={selectedIndex === index}
130
+ * onMouseEnter={() => {onNavigateTo(index)}}
131
+ * onSelect={() => {onSelect()}}
132
+ * />
133
+ * ```
134
+ *
135
+ * Note: The currently selected match is automatically inserted on Enter or
136
+ * Tab.
137
+ */
138
+ onSelect: () => void
139
+ /**
140
+ * Dismiss the emoji picker. Can be used to let the user dismiss the picker
141
+ * by clicking a button.
142
+ *
143
+ * @example
144
+ * ```tsx
145
+ * {matches.length === 0 ? (
146
+ * <Button onPress={onDismiss}>Dismiss</Button>
147
+ * ) : <EmojiListBox {...props} />}
148
+ * ```
149
+ *
150
+ * Note: The emoji picker is automatically dismissed on Escape.
151
+ */
152
+ onDismiss: () => void
153
+ }
154
+
155
+ /**
156
+ * @beta
157
+ */
158
+ export declare type EmojiPickerProps<TEmojiMatch = EmojiMatch> = {
159
+ matchEmojis: MatchEmojis<TEmojiMatch>
160
+ }
161
+
162
+ /**
163
+ * A function that returns an array of emoji matches for a given keyword.
164
+ *
165
+ * @beta
166
+ */
167
+ export declare type MatchEmojis<TEmojiMatch = EmojiMatch> = (query: {
168
+ keyword: string
169
+ }) => ReadonlyArray<TEmojiMatch>
170
+
171
+ /**
172
+ * Proposed, but not required, default implementation of `MatchEmojis`.
173
+ *
174
+ * @beta
175
+ */
176
+ export declare const matchEmojis: MatchEmojis
177
+
178
+ /**
179
+ * Handles the state and logic needed to create an emoji picker.
180
+ *
181
+ * The `matchEmojis` function is generic and can return any shape of emoji
182
+ * match required for the emoji picker.
183
+ *
184
+ * However, the default implementation of `matchEmojis` returns an array of
185
+ * `EmojiMatch` objects and can be created using the `createMatchEmojis`
186
+ * function.
187
+ *
188
+ * @example
189
+ *
190
+ * ```tsx
191
+ * const matchEmojis = createMatchEmojis({emojis: {
192
+ * '😂': ['joy'],
193
+ * '😹': ['joy_cat'],
194
+ * }})
195
+ *
196
+ * const {keyword, matches, selectedIndex, onDismiss, onNavigateTo, onSelect} =
197
+ * useEmojiPicker({matchEmojis})
198
+ * ```
199
+ *
200
+ * Note: This hook is not concerned with the UI, how the emoji picker is
201
+ * rendered or positioned in the document.
202
+ *
203
+ * @beta
204
+ */
205
+ export declare function useEmojiPicker<TEmojiMatch = EmojiMatch>(
206
+ props: EmojiPickerProps<TEmojiMatch>,
207
+ ): EmojiPicker<TEmojiMatch>
208
+
209
+ export {}
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Proposed, but not required, function to create a `MatchEmojis` function.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * const matchEmojis = createMatchEmojis({
7
+ * emojis: {
8
+ * '😂': ['joy'],
9
+ * '😹': ['joy_cat'],
10
+ * },
11
+ * })
12
+ * ```
13
+ *
14
+ * @beta
15
+ */
16
+ export declare function createMatchEmojis(config: {
17
+ emojis: Record<string, ReadonlyArray<string>>
18
+ }): MatchEmojis
19
+
20
+ /**
21
+ * Proposed, but not required type, to represent an emoji match.
22
+ *
23
+ * @example
24
+ * ```tsx
25
+ * {
26
+ * type: 'exact',
27
+ * key: '😂-joy',
28
+ * emoji: '😂',
29
+ * keyword: 'joy',
30
+ * }
31
+ * ```
32
+ * @example
33
+ * ```tsx
34
+ * {
35
+ * type: 'partial',
36
+ * key: '😹-joy-_cat',
37
+ * emoji: '😹',
38
+ * keyword: 'joy',
39
+ * startSlice: '',
40
+ * endSlice: '_cat',
41
+ * }
42
+ * ```
43
+ *
44
+ * @beta
45
+ */
46
+ export declare type EmojiMatch =
47
+ | {
48
+ type: 'exact'
49
+ key: string
50
+ emoji: string
51
+ keyword: string
52
+ }
53
+ | {
54
+ type: 'partial'
55
+ key: string
56
+ emoji: string
57
+ keyword: string
58
+ startSlice: string
59
+ endSlice: string
60
+ }
61
+
62
+ /**
63
+ * @beta
64
+ */
65
+ export declare type EmojiPicker<TEmojiMatch = EmojiMatch> = {
66
+ /**
67
+ * The matched keyword, including colons.
68
+ *
69
+ * Can be used to display the keyword in the UI or conditionally render the
70
+ * list of matches.
71
+ *
72
+ * @example
73
+ * ```tsx
74
+ * if (keyword.length < 2) {
75
+ * return null
76
+ * }
77
+ * ```
78
+ */
79
+ keyword: string
80
+ /**
81
+ * Emoji matches found for the current keyword.
82
+ *
83
+ * Can be used to display the matches in a list.
84
+ */
85
+ matches: ReadonlyArray<TEmojiMatch>
86
+ /**
87
+ * The index of the selected match.
88
+ *
89
+ * Can be used to highlight the selected match in the list.
90
+ *
91
+ * @example
92
+ * ```tsx
93
+ * <EmojiListItem
94
+ * key={match.key}
95
+ * match={match}
96
+ * selected={selectedIndex === index}
97
+ * />
98
+ * ```
99
+ */
100
+ selectedIndex: number
101
+ /**
102
+ * Navigate to a specific match by index.
103
+ *
104
+ * Can be used to control the `selectedIndex`. For example, using
105
+ * `onMouseEnter`.
106
+ *
107
+ * @example
108
+ * ```tsx
109
+ * <EmojiListItem
110
+ * key={match.key}
111
+ * match={match}
112
+ * selected={selectedIndex === index}
113
+ * onMouseEnter={() => {onNavigateTo(index)}}
114
+ * />
115
+ * ```
116
+ */
117
+ onNavigateTo: (index: number) => void
118
+ /**
119
+ * Select the current match.
120
+ *
121
+ * Can be used to insert the currently selected match.
122
+ *
123
+ *
124
+ * @example
125
+ * ```tsx
126
+ * <EmojiListItem
127
+ * key={match.key}
128
+ * match={match}
129
+ * selected={selectedIndex === index}
130
+ * onMouseEnter={() => {onNavigateTo(index)}}
131
+ * onSelect={() => {onSelect()}}
132
+ * />
133
+ * ```
134
+ *
135
+ * Note: The currently selected match is automatically inserted on Enter or
136
+ * Tab.
137
+ */
138
+ onSelect: () => void
139
+ /**
140
+ * Dismiss the emoji picker. Can be used to let the user dismiss the picker
141
+ * by clicking a button.
142
+ *
143
+ * @example
144
+ * ```tsx
145
+ * {matches.length === 0 ? (
146
+ * <Button onPress={onDismiss}>Dismiss</Button>
147
+ * ) : <EmojiListBox {...props} />}
148
+ * ```
149
+ *
150
+ * Note: The emoji picker is automatically dismissed on Escape.
151
+ */
152
+ onDismiss: () => void
153
+ }
154
+
155
+ /**
156
+ * @beta
157
+ */
158
+ export declare type EmojiPickerProps<TEmojiMatch = EmojiMatch> = {
159
+ matchEmojis: MatchEmojis<TEmojiMatch>
160
+ }
161
+
162
+ /**
163
+ * A function that returns an array of emoji matches for a given keyword.
164
+ *
165
+ * @beta
166
+ */
167
+ export declare type MatchEmojis<TEmojiMatch = EmojiMatch> = (query: {
168
+ keyword: string
169
+ }) => ReadonlyArray<TEmojiMatch>
170
+
171
+ /**
172
+ * Proposed, but not required, default implementation of `MatchEmojis`.
173
+ *
174
+ * @beta
175
+ */
176
+ export declare const matchEmojis: MatchEmojis
177
+
178
+ /**
179
+ * Handles the state and logic needed to create an emoji picker.
180
+ *
181
+ * The `matchEmojis` function is generic and can return any shape of emoji
182
+ * match required for the emoji picker.
183
+ *
184
+ * However, the default implementation of `matchEmojis` returns an array of
185
+ * `EmojiMatch` objects and can be created using the `createMatchEmojis`
186
+ * function.
187
+ *
188
+ * @example
189
+ *
190
+ * ```tsx
191
+ * const matchEmojis = createMatchEmojis({emojis: {
192
+ * '😂': ['joy'],
193
+ * '😹': ['joy_cat'],
194
+ * }})
195
+ *
196
+ * const {keyword, matches, selectedIndex, onDismiss, onNavigateTo, onSelect} =
197
+ * useEmojiPicker({matchEmojis})
198
+ * ```
199
+ *
200
+ * Note: This hook is not concerned with the UI, how the emoji picker is
201
+ * rendered or positioned in the document.
202
+ *
203
+ * @beta
204
+ */
205
+ export declare function useEmojiPicker<TEmojiMatch = EmojiMatch>(
206
+ props: EmojiPickerProps<TEmojiMatch>,
207
+ ): EmojiPicker<TEmojiMatch>
208
+
209
+ export {}