Support providing a fallback to unsupported emojis [Fix #157]

release
Etienne Lemay 2018-01-09 14:19:57 -05:00
parent d17c8a5c4b
commit 3fa0331d60
3 changed files with 40 additions and 13 deletions

View File

@ -149,12 +149,29 @@ import { Emoji } from 'emoji-mart'
| **onClick** | | | Params: `(emoji, event) => {}` | | **onClick** | | | Params: `(emoji, event) => {}` |
| **onLeave** | | | Params: `(emoji, event) => {}` | | **onLeave** | | | Params: `(emoji, event) => {}` |
| **onOver** | | | Params: `(emoji, event) => {}` | | **onOver** | | | Params: `(emoji, event) => {}` |
| [**fallback**](#unsupported-emojis-fallback) | | | Params: `(emoji) => {}` |
| **set** | | `apple` | The emoji set: `'apple', 'google', 'twitter', 'emojione'` | | **set** | | `apple` | The emoji set: `'apple', 'google', 'twitter', 'emojione'` |
| **sheetSize** | | `64` | The emoji [sheet size](#sheet-sizes): `16, 20, 32, 64` | | **sheetSize** | | `64` | The emoji [sheet size](#sheet-sizes): `16, 20, 32, 64` |
| **backgroundImageFn** | | ```((set, sheetSize) => `https://unpkg.com/emoji-datasource@3.0.0/sheet_${set}_${sheetSize}.png`)``` | A Fn that returns that image sheet to use for emojis. Useful for avoiding a request if you have the sheet locally. | | **backgroundImageFn** | | ```((set, sheetSize) => `https://unpkg.com/emoji-datasource@3.0.0/sheet_${set}_${sheetSize}.png`)``` | A Fn that returns that image sheet to use for emojis. Useful for avoiding a request if you have the sheet locally. |
| **skin** | | `1` | Skin color: `1, 2, 3, 4, 5, 6` | | **skin** | | `1` | Skin color: `1, 2, 3, 4, 5, 6` |
| **tooltip** | | `false` | Show emoji short name when hovering (title) | | **tooltip** | | `false` | Show emoji short name when hovering (title) |
#### Unsupported emojis fallback
Certain sets dont support all emojis (i.e. Messenger & Facebook dont support `:shrug:`). By default the Emoji component will not render anything so that the emojis dont take space in the picker when not available. When using the standalone Emoji component, you can however render anything you want by providing the `fallback` props.
To have the component render `:shrug:` you would need to:
```js
<Emoji
set={'messenger'}
emoji={'shrug'}
size={24}
fallback={(emoji) => {
return `:${emoji.short_names[0]}:`
}}
/>
```
## Custom emojis ## Custom emojis
You can provide custom emojis which will show up in their own category. You can provide custom emojis which will show up in their own category.

View File

@ -102,19 +102,24 @@ const Emoji = props => {
let setHasEmoji = _getData(props)[`has_img_${props.set}`] let setHasEmoji = _getData(props)[`has_img_${props.set}`]
if (!setHasEmoji) { if (!setHasEmoji) {
return null if (props.fallback) {
} style = { fontSize: props.size }
children = props.fallback(data)
style = { } else {
width: props.size, return null
height: props.size, }
display: 'inline-block', } else {
backgroundImage: `url(${props.backgroundImageFn( style = {
props.set, width: props.size,
props.sheetSize height: props.size,
)})`, display: 'inline-block',
backgroundSize: `${100 * SHEET_COLUMNS}%`, backgroundImage: `url(${props.backgroundImageFn(
backgroundPosition: _getPosition(props), props.set,
props.sheetSize
)})`,
backgroundSize: `${100 * SHEET_COLUMNS}%`,
backgroundPosition: _getPosition(props),
}
} }
} }
@ -136,6 +141,7 @@ Emoji.propTypes = {
onOver: PropTypes.func, onOver: PropTypes.func,
onLeave: PropTypes.func, onLeave: PropTypes.func,
onClick: PropTypes.func, onClick: PropTypes.func,
fallback: PropTypes.func,
backgroundImageFn: PropTypes.func, backgroundImageFn: PropTypes.func,
native: PropTypes.bool, native: PropTypes.bool,
forceSize: PropTypes.bool, forceSize: PropTypes.bool,
@ -166,6 +172,7 @@ Emoji.defaultProps = {
onOver: () => {}, onOver: () => {},
onLeave: () => {}, onLeave: () => {},
onClick: () => {}, onClick: () => {},
fallback: (emoji) => {},
} }
export default Emoji export default Emoji

View File

@ -50,6 +50,9 @@ storiesOf('Emoji', module)
emoji={text('Emoji', '+1')} emoji={text('Emoji', '+1')}
size={number('Emoji size', 64)} size={number('Emoji size', 64)}
skin={number('Skin tone', 1)} skin={number('Skin tone', 1)}
fallback={(emoji) => {
return `:${emoji.short_names[0]}:`
}}
/> />
)); ));