An open source design system that's fully customizable and agent ready
TypeScript
// Copyright (c) Meta Platforms, Inc. and affiliates.
'use client';
/**
* @file ChatEmojiPicker.tsx
* @input Uses React, StyleX, theme tokens, Popover, TextInput, useGridFocus
* @output Exports ChatEmojiPicker component, ChatEmojiPickerProps,
* ChatEmojiOption, and DEFAULT_CHAT_EMOJIS
* @position Popover emoji grid for chat reactions; wraps a trigger button
*
* Renders a Popover around a trigger element: a shortname filter input over
* a fixed 8-column emoji grid. Arrow keys move focus through the grid
* (left/right by one, up/down by a row) via useGridFocus; picking an emoji
* calls onSelect and closes the popover, which restores focus to the
* trigger. Ships with a small default emoji set; pass `emojis` to override.
*
* SYNC: When modified, update these files to stay in sync:
* - /packages/lab/src/Chat/index.ts (exports)
* - /packages/lab/src/Chat/ChatEmojiPicker.doc.mjs
* - /apps/storybook/stories/ChatAdditions.stories.tsx (examples)
*/
import type {ReactNode} from 'react';
import {useState} from 'react';
import * as stylex from '@stylexjs/stylex';
import {
colorVars,
spacingVars,
radiusVars,
durationVars,
easeVars,
typographyVars,
typeScaleVars,
} from '@astryxdesign/core/theme/tokens.stylex';
import type {BaseProps} from '@astryxdesign/core';
import {focusOutlineStyles, mergeProps} from '@astryxdesign/core/utils';
import {themeProps} from '@astryxdesign/core/utils';
import {Popover} from '@astryxdesign/core/Popover';
import {TextInput} from '@astryxdesign/core/TextInput';
import {useGridFocus} from '@astryxdesign/core/hooks';
export interface ChatEmojiOption {
/** The emoji character, e.g. "๐". */
emoji: string;
/** Shortname used for filtering and accessible labels, e.g. "tada". */
name: string;
}
/**
* Small default emoji set (two grid rows) covering the common
* reaction vocabulary. Override via the `emojis` prop.
*/
export const DEFAULT_CHAT_EMOJIS: ReadonlyArray<ChatEmojiOption> = [
{emoji: '๐', name: 'thumbs up'},
{emoji: 'โค๏ธ', name: 'heart'},
{emoji: '๐', name: 'joy'},
{emoji: '๐', name: 'tada'},
{emoji: '๐ฎ', name: 'wow'},
{emoji: '๐ข', name: 'cry'},
{emoji: '๐ฅ', name: 'fire'},
{emoji: '๐', name: 'eyes'},
{emoji: 'โ
', name: 'check'},
{emoji: '๐', name: 'pray'},
{emoji: '๐ฏ', name: 'hundred'},
{emoji: '๐', name: 'rocket'},
{emoji: '๐', name: 'heart eyes'},
{emoji: '๐ค', name: 'thinking'},
{emoji: '๐', name: 'wave'},
{emoji: 'โญ', name: 'star'},
];
const GRID_COLUMNS = 8;
export interface ChatEmojiPickerProps extends Omit<
BaseProps<HTMLDivElement>,
'onSelect'
> {
/** Ref forwarded to the popover panel element */
ref?: React.Ref<HTMLDivElement>;
/**
* Called with the picked emoji character.
* The popover closes itself after selection.
*/
onSelect: (emoji: string) => void;
/**
* Emoji options rendered in the grid (8 per row).
* @default DEFAULT_CHAT_EMOJIS
*/
emojis?: ReadonlyArray<ChatEmojiOption>;
/**
* Accessible label for the popover dialog.
* @default 'Pick an emoji'
*/
label?: string;
/**
* Placeholder and hidden label for the filter input.
* @default 'Search emoji'
*/
searchLabel?: string;
/**
* Trigger element โ must contain a `<button>` (Popover wires it up).
*/
children: ReactNode;
/** Test ID for the popover content. */
'data-testid'?: string;
}
// =============================================================================
// Styles
// =============================================================================
const styles = stylex.create({
panel: {
display: 'flex',
flexDirection: 'column',
gap: spacingVars['--spacing-2'],
width: GRID_COLUMNS * 34 + 8,
boxSizing: 'border-box',
},
grid: {
display: 'grid',
gridTemplateColumns: `repeat(${GRID_COLUMNS}, 1fr)`,
gap: spacingVars['--spacing-0-5'],
},
cell: {
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
width: 32,
height: 32,
borderRadius: radiusVars['--radius-inner'],
borderStyle: 'none',
padding: 0,
backgroundColor: {
default: 'transparent',
':hover:where(:not(:disabled,[aria-disabled="true"]))': {
default: 'transparent',
'@media (hover: hover)': colorVars['--color-overlay-hover'],
},
},
cursor: {
default: 'pointer',
':is(:disabled,[aria-disabled="true"])': 'default',
},
fontSize: 18,
lineHeight: typeScaleVars['--text-body-leading'],
// Inset, not the shared offset: the cells sit in a tight 8-across grid, so
// an outset ring would overlap its neighbors.
outlineOffset: {default: '0', ':focus-visible': -2},
transitionProperty: 'background-color',
transitionDuration: {
default: durationVars['--duration-fast'],
'@media (prefers-reduced-motion: reduce)': '0s',
},
transitionTimingFunction: easeVars['--ease-standard'],
},
empty: {
paddingBlock: spacingVars['--spacing-2'],
paddingInline: spacingVars['--spacing-1'],
fontFamily: typographyVars['--font-family-body'],
fontSize: typeScaleVars['--text-supporting-size'],
color: colorVars['--color-text-secondary'],
},
});
// =============================================================================
// Component
// =============================================================================
/**
* Popover emoji grid with shortname filtering and arrow-key navigation.
*
* Wraps its trigger button in a Popover. Typing in the filter input narrows
* the grid by shortname; arrow keys move focus between emoji; picking one
* calls `onSelect` and closes the popover (focus returns to the trigger).
*
* @example
* ```
* <ChatEmojiPicker onSelect={(emoji) => addReaction(emoji)}>
* <button type="button" aria-label="Add reaction">๐</button>
* </ChatEmojiPicker>
* ```
*/
export function ChatEmojiPicker({
onSelect,
emojis = DEFAULT_CHAT_EMOJIS,
label = 'Pick an emoji',
searchLabel = 'Search emoji',
children,
xstyle,
className,
style: styleProp,
'data-testid': testId,
ref,
}: ChatEmojiPickerProps) {
const [isOpen, setIsOpen] = useState(false);
const [query, setQuery] = useState('');
const {gridRef, handleKeyDown} = useGridFocus<HTMLDivElement>({
columns: GRID_COLUMNS,
});
const trimmed = query.trim().toLowerCase();
const visible =
trimmed === ''
? emojis
: emojis.filter(
option =>
option.name.toLowerCase().includes(trimmed) ||
option.emoji === trimmed,
);
const handleOpenChange = (open: boolean) => {
setIsOpen(open);
if (!open) {
setQuery('');
}
};
const handleSelect = (emoji: string) => {
onSelect(emoji);
handleOpenChange(false);
};
return (
<Popover
isOpen={isOpen}
onOpenChange={handleOpenChange}
label={label}
placement="below"
alignment="start"
data-testid={testId}
content={
<div
ref={ref}
{...mergeProps(
themeProps('chat-emoji-picker'),
stylex.props(styles.panel, xstyle),
className,
styleProp,
)}>
<TextInput
label={searchLabel}
isLabelHidden
size="sm"
placeholder={searchLabel}
value={query}
onChange={setQuery}
hasClear
/>
{visible.length === 0 ? (
<div {...stylex.props(styles.empty)}>
No emoji match โ{query.trim()}โ.
</div>
) : (
<div
ref={gridRef}
role="group"
aria-label="Emoji"
onKeyDown={handleKeyDown}
{...stylex.props(styles.grid)}>
{visible.map(option => (
<button
key={option.emoji}
type="button"
data-emoji={option.emoji}
aria-label={`React with ${option.name}`}
onClick={() => handleSelect(option.emoji)}
{...stylex.props(
focusOutlineStyles.focusVisible,
styles.cell,
)}>
<span aria-hidden="true">{option.emoji}</span>
</button>
))}
</div>
)}
</div>
}>
{children}
</Popover>
);
}
ChatEmojiPicker.displayName = 'ChatEmojiPicker';