Emoji reactions under a message, with a picker to add one.
Mira Patel · 10:14 AM
Invoices v2 is live for every EU workspace. VAT shows once now.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Reaction, Reactions, toggleReaction } from "./ui/uicontext/components/kit/Reactions.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ frame: { display: "grid", gap: 10, inlineSize: "min(100%, 30rem)" }, text: { margin: 0, fontSize: 15, lineHeight: 1.6, color: color.fg }, meta: { fontSize: font.sm, color: color.faint },})const Message = UiSchema.TaggedEnum({ ToggledReaction: { emoji: Schema.String, on: Schema.Boolean }, ToggledReactionPicker: { open: Schema.Boolean },})export const ShipReactions = Html.component({ name: "ShipReactions", model: Schema.Struct({ reactions: Schema.Array(Reaction), picker: Schema.Boolean }), message: Message,}).make({ init: () => ({ picker: false, reactions: [ { emoji: "🚀", count: 5, mine: false, names: ["Ben Okafor", "Mira Patel", "Scout", "Grace Hopper", "Alan Turing"] }, { emoji: "🎉", count: 2, mine: true, names: ["Ada Lovelace", "Mira Patel"] }, { emoji: "👀", count: 1, mine: false, names: ["Ben Okafor"] }, ], }), update: { // counts change at once; the app sends the reaction and corrects it if the server disagrees ToggledReaction: ($, { emoji, on }) => $.evolve({ reactions: (all) => toggleReaction(all, emoji, on, "Ada Lovelace"), picker: false }), ToggledReactionPicker: ($, { open }) => $.evolve({ picker: open }), }, view: (h, { reactions, picker }) => h.div.attrs(sx(styles.frame)).children( h.span.attrs(sx(styles.meta)).children("Mira Patel · 10:14 AM"), h.p.attrs(sx(styles.text)).children("Invoices v2 is live for every EU workspace. VAT shows once now."), Reactions(h, { reactions, onToggle: (emoji, on) => Message.ToggledReaction({ emoji, on }), picker: { id: "ship-reactions", open: picker, onToggle: (open) => Message.ToggledReactionPicker({ open }) }, }), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/reactionsUsage
import { Reactions } from "./ui/uicontext/components/kit/Reactions.ts"import type { Reaction } from "./ui/uicontext/components/kit/Reactions.ts"const Message = UiSchema.TaggedEnum({ ToggledReaction: { emoji: Schema.String, on: Schema.Boolean }, ToggledPicker: { open: Schema.Boolean },})// update with `toggleReaction(reactions, emoji, on, me)`, then send it to your serverexport const MessageReactions = (h: Html.H<typeof Message.Type>, reactions: ReadonlyArray<Reaction>, pickerOpen: boolean) => Reactions(h, { reactions, onToggle: (emoji, on) => Message.ToggledReaction({ emoji, on }), picker: { id: "message-42-reactions", open: pickerOpen, onToggle: (open) => Message.ToggledPicker({ open }) }, })Examples
Read-only
Without react in can, the chips still say who reacted, but can't be pressed, and there's no picker.
Omar Haddad · Nov 14 · archived
The Lisbon office closes for the holidays from December 23 to January 2.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Reaction, Reactions } from "./ui/uicontext/components/kit/Reactions.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ frame: { display: "grid", gap: 10, inlineSize: "min(100%, 30rem)" }, text: { margin: 0, fontSize: 15, lineHeight: 1.6, color: color.fg }, meta: { fontSize: font.sm, color: color.faint },})const Message = UiSchema.TaggedEnum({ ToggledReaction: { emoji: Schema.String, on: Schema.Boolean } })// An archived announcement: everyone sees who reacted, nobody can change itexport const ArchivedReactions = Html.component({ name: "ArchivedReactions", model: Schema.Struct({ reactions: Schema.Array(Reaction) }), message: Message,}).make({ init: () => ({ reactions: [ { emoji: "🎉", count: 12, mine: true, names: ["Ada Lovelace", "Lena Fischer", "Mira Patel"] }, { emoji: "🙏", count: 4, mine: false, names: ["Ben Okafor", "Grace Hopper", "Alan Turing", "Maya Chen"] }, ], }), // the chips are disabled, so this never arrives while the message is archived update: { ToggledReaction: ($) => $.model }, view: (h, { reactions }) => h.div.attrs(sx(styles.frame)).children( h.span.attrs(sx(styles.meta)).children("Omar Haddad · Nov 14 · archived"), h.p.attrs(sx(styles.text)).children("The Lisbon office closes for the holidays from December 23 to January 2."), Reactions(h, { can: [], reactions, onToggle: (emoji, on) => Message.ToggledReaction({ emoji, on }) }), ),})API reference
ReactionChipsView function
Options
| Name | Type | Description |
|---|---|---|
reactionsrequired | Reaction[] | Each emoji with its count, who reacted, and whether the reader did |
chiprequired | (button: ButtonBuilder<Message>, emoji: string, on: boolean) => ButtonBuilder<Message> | Binds each chip's press: gets the button, the emoji, and whether pressing adds the reader's reaction |
disabled | boolean | Shows the chips without letting the reader press them |
ReactionGridView function
Options
| Name | Type | Description |
|---|---|---|
mine | string[] | What the reader has already reacted with, shown chosen |
choices | string[] | The emoji to offer; a common set by default |
choicerequired | (button: ButtonBuilder<Message>, emoji: string, on: boolean) => ButtonBuilder<Message> | Binds each emoji's press: gets the button, the emoji, and whether pressing adds it |
ReactionPickerView function
Options
| Name | Type | Description |
|---|---|---|
idrequired | string | Unique on the page: the popover's id |
openrequired | boolean | Whether the grid shows |
onTogglerequired | (open: boolean) => Message | The browser opened or closed it (its button, Escape, a click outside) |
onPickrequired | (emoji: string, on: boolean) => Message | The reader picked an emoji: add theirs, or take it back if it's already theirs |
mine | string[] | What the reader has already reacted with, shown chosen |
choices | string[] | The emoji to offer; a common set by default |
ReactionsView function
Options
| Name | Type | Description |
|---|---|---|
reactionsrequired | Reaction[] | Each emoji with its count, who reacted, and whether the reader did |
onTogglerequired | (emoji: string, on: boolean) => Message | The reader pressed a chip: on adds theirs, false takes it back |
picker | Omit<ReactionPickerOptions<Message>, "onPick" | "mine"> | A picker after the chips, to add a new one |
can | ReadonlyArray<"react"> | "react" when the reader may react; read-only chips without it |
Accessibility
- Each reaction is a toggle button (
aria-pressed) that says the emoji, the count and who reacted, including you. - Add a reaction opens a grid of emoji buttons in a popover that closes with Escape; emoji you've already used are marked pressed.