Reaction Pill
A toggleable emoji reaction pill, plus the group that rows them under a message.
A toggleable emoji reaction pill, plus the group that rows them under a message. Compose it from the exported registry parts so local content and behaviour stay in the application while the visual contract stays token-driven.
newt-reactionimport { Reaction, ReactionGroup } from "@/registry/bases/newt/ui/reaction"
export default function ReactionDemo() {
return (
<ReactionGroup>
<Reaction emoji="👍" count={12} defaultActive />
<Reaction emoji="🚀" count={4} />
</ReactionGroup>
)
}<script setup lang="ts">
import { Reaction, ReactionGroup } from "~~/registry/bases/newt/ui/reaction"
</script>
<template>
<ReactionGroup>
<Reaction emoji="👍" :count="12" default-active />
<Reaction emoji="🚀" :count="4" />
</ReactionGroup>
</template>Installation
npx newtui@latest add reactionnpx shadcn@latest add @newtui/reactionnpx shadcn-vue@latest add @newtui/reactionpnpm dlx newtui@latest add reactionpnpm dlx shadcn@latest add @newtui/reactionpnpm dlx shadcn-vue@latest add @newtui/reactionyarn dlx newtui@latest add reactionyarn dlx shadcn@latest add @newtui/reactionyarn dlx shadcn-vue@latest add @newtui/reactionbunx newtui@latest add reactionbunx shadcn@latest add @newtui/reactionbunx shadcn-vue@latest add @newtui/reactiondeno run -A npm:newtui@latest add reactiondeno run -A npm:shadcn@latest add @newtui/reactiondeno run -A npm:shadcn-vue@latest add @newtui/reactionvlx newtui@latest add reactionvlx shadcn@latest add @newtui/reactionvlx shadcn-vue@latest add @newtui/reactionvp dlx newtui@latest add reactionvp dlx shadcn@latest add @newtui/reactionvp dlx shadcn-vue@latest add @newtui/reactionnubx newtui@latest add reactionnubx shadcn@latest add @newtui/reactionnubx shadcn-vue@latest add @newtui/reactionnlx newtui@latest add reactionnlx shadcn@latest add @newtui/reactionnlx shadcn-vue@latest add @newtui/reactionupx newtui@latest add reactionupx shadcn@latest add @newtui/reactionupx shadcn-vue@latest add @newtui/reaction- newtui
- shadcn
- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
The shadcn CLI needs the @newtui registry in your components.json. Set it up.
Install the dependencies
npm install class-variance-authoritypnpm add class-variance-authorityyarn add class-variance-authoritybun add class-variance-authoritydeno add npm:class-variance-authorityvlt install class-variance-authorityvp add class-variance-authoritynub add class-variance-authorityni class-variance-authorityupm add class-variance-authority- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
Copy the source into your project
"use client"
import { cva, type VariantProps } from "class-variance-authority"
import * as React from "react"
import { cn } from "@/lib/utils"
const reactionVariants = cva(
"inline-flex items-center gap-1 rounded-[var(--newt-radius-md)] border px-2 py-0.5 text-[13px] transition-[background-color,border-color] duration-fast ease-newt focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link",
{
variants: {
active: {
false: "border-newt-border bg-newt-bg-surface text-newt-text-secondary",
true: "border-newt-brand bg-[color-mix(in_srgb,var(--newt-brand)_15%,transparent)] text-newt-mention-text",
},
/* `false` for a read-only reaction: no pointer, no hover. */
interactive: {
true: "cursor-pointer",
false: "cursor-[inherit]",
},
},
compoundVariants: [
{ active: false, interactive: true, class: "hover:bg-newt-bg-hover" },
],
defaultVariants: {
active: false,
interactive: true,
},
}
)
function isStringNode(value: React.ReactNode): value is string {
return Object.prototype.toString.call(value) === "[object String]"
}
/** What the accessible name of a reaction is built from. */
export interface ReactionLabelState {
/** The emoji's text, or `emojiAlt` for an image emoji, or `"Emoji"`. */
emoji: string
count: number
active: boolean
}
/** The English default. Pass `formatLabel` to translate it. */
export function defaultReactionLabel({
emoji,
count,
active,
}: ReactionLabelState): string {
return `${emoji} reaction, ${count} ${count === 1 ? "person" : "people"}${
active ? ", you reacted" : ""
}`
}
export interface ReactionProps
extends
React.ButtonHTMLAttributes<HTMLButtonElement>,
Omit<VariantProps<typeof reactionVariants>, "interactive"> {
/**
* The emoji, drawn by your own component: an icon or an image component.
* Wins over `emoji` and `emojiSrc`. Set `emojiAlt` to name it.
*/
children?: React.ReactNode
/** Emoji or icon rendered on the left. Optional when `emojiSrc` is set. */
emoji?: React.ReactNode
/** Image URL of a server (custom) emoji, rendered instead of `emoji`. */
emojiSrc?: string
/** Name of the emoji, for the accessible name: `:wolf:` or `wolf`. */
emojiAlt?: string
/** Number of users who reacted. */
count: number
/** Controlled active state. */
active?: boolean
/** Initial active state when uncontrolled. */
defaultActive?: boolean
/** Called with the next active state when toggled. */
onActiveChange?: (active: boolean) => void
/**
* Show the count without a toggle: renders a `<span>` that is not focusable
* and ignores clicks, for feeds that only display reactions.
*/
readonly?: boolean
/** Builds the accessible name; `aria-label` still wins when both are set. */
formatLabel?: (state: ReactionLabelState) => string
}
/* Widens the tag so one ref type serves both elements it can be. */
function asElementType(element: "span" | "button"): React.ElementType {
return element
}
const Reaction = React.forwardRef<HTMLElement, ReactionProps>(
(
{
className,
children,
emoji,
emojiSrc,
emojiAlt,
count,
active: activeProp,
defaultActive = false,
onActiveChange,
onClick,
readonly = false,
formatLabel = defaultReactionLabel,
"aria-label": ariaLabel,
...props
},
ref
) => {
const [uncontrolled, setUncontrolled] = React.useState(defaultActive)
const isControlled = activeProp !== undefined
const active = isControlled ? activeProp : uncontrolled
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
onClick?.(event)
if (event.defaultPrevented) return
const next = !active
if (!isControlled) setUncontrolled(next)
onActiveChange?.(next)
}
const label =
ariaLabel ??
formatLabel({
emoji: emojiAlt ?? (isStringNode(emoji) ? emoji : "Emoji"),
count,
active,
})
const content = (
<>
{React.Children.toArray(children).length > 0 ? (
<span
aria-hidden="true"
className="inline-flex shrink-0 items-center"
>
{children}
</span>
) : emojiSrc ? (
<img
src={emojiSrc}
alt=""
aria-hidden="true"
draggable={false}
className="h-4 w-4 object-contain"
/>
) : (
<span aria-hidden="true">{emoji}</span>
)}
<span aria-hidden="true" className="text-xs font-semibold">
{count}
</span>
</>
)
/*
* Read-only renders a `<span>`: it keeps every attribute a caller passes
* (`style`, `data-*`, `aria-describedby`, handlers other than `onClick`)
* but drops the ones only a button understands, and never toggles.
*/
const Tag = asElementType(readonly ? "span" : "button")
const {
type: _type,
disabled: _disabled,
form: _form,
formAction: _formAction,
formEncType: _formEncType,
formMethod: _formMethod,
formNoValidate: _formNoValidate,
formTarget: _formTarget,
name: _name,
value: _value,
...shared
} = props
const elementProps = readonly
? { role: "img", ...shared }
: {
type: "button",
"aria-pressed": active,
onClick: handleClick,
...props,
}
return (
<Tag
ref={ref}
aria-label={label}
data-state={active ? "active" : "inactive"}
className={cn(
reactionVariants({ active, interactive: !readonly }),
className
)}
{...elementProps}
>
{content}
</Tag>
)
}
)
Reaction.displayName = "Reaction"
/* The row of reactions under a message. */
const ReactionGroup = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("mt-1 flex flex-wrap items-center gap-1", className)}
{...props}
/>
))
ReactionGroup.displayName = "ReactionGroup"
export { Reaction, ReactionGroup, reactionVariants }<script setup lang="ts">
import type { ButtonHTMLAttributes } from "vue"
import { computed, ref, watch, type VNode } from "vue"
import { cn } from "@/lib/utils"
import {
defaultReactionLabel,
reactionVariants,
type ReactionLabelState,
} from "."
const props = withDefaults(
defineProps<{
class?: ButtonHTMLAttributes["class"]
/** Emoji or short text rendered on the left. Optional with `emojiSrc`. */
emoji?: string
/** Image URL of a server (custom) emoji, rendered instead of `emoji`. */
emojiSrc?: string
/** Name of the emoji, for the accessible name: `:wolf:` or `wolf`. */
emojiAlt?: string
/** Number of users who reacted. */
count: number
/** Controlled active state (use with v-model:active). */
active?: boolean
/** Initial active state when uncontrolled. */
defaultActive?: boolean
/**
* Show the count without a toggle: renders a `<span>` that is not
* focusable and ignores clicks, for feeds that only display reactions.
*/
readonly?: boolean
/** Fixed accessible name. Wins over `formatLabel`. */
ariaLabel?: string
/** Builds the accessible name from the emoji, count and state. */
formatLabel?: (state: ReactionLabelState) => string
}>(),
{
active: undefined,
defaultActive: false,
readonly: false,
formatLabel: defaultReactionLabel,
}
)
const slots = defineSlots<{
/**
* The emoji, drawn by your own component: an icon or an image component.
* Wins over `emoji` and `emojiSrc`. Set `emojiAlt` to name it.
*/
default?: () => VNode[]
}>()
const emit = defineEmits<{ (e: "update:active", value: boolean): void }>()
const internal = ref(props.active ?? props.defaultActive)
watch(
() => props.active,
(v) => {
if (v !== undefined) internal.value = v
}
)
const isActive = computed(() =>
props.active !== undefined ? props.active : internal.value
)
function toggle() {
const next = !isActive.value
if (props.active === undefined) internal.value = next
emit("update:active", next)
}
const label = computed(
() =>
props.ariaLabel ??
props.formatLabel({
emoji: props.emojiAlt ?? props.emoji ?? "Emoji",
count: props.count,
active: isActive.value,
})
)
</script>
<template>
<span
v-if="props.readonly"
role="img"
:aria-label="label"
:data-state="isActive ? 'active' : 'inactive'"
:class="
cn(
reactionVariants({ active: isActive, interactive: false }),
props.class
)
"
>
<span
v-if="slots.default"
aria-hidden="true"
class="inline-flex shrink-0 items-center"
>
<slot />
</span>
<img
v-else-if="props.emojiSrc"
:src="props.emojiSrc"
alt=""
aria-hidden="true"
:draggable="false"
class="h-4 w-4 object-contain"
/>
<span v-else aria-hidden="true">{{ props.emoji }}</span>
<span aria-hidden="true" class="text-xs font-semibold">{{
props.count
}}</span>
</span>
<button
v-else
type="button"
:aria-pressed="isActive"
:aria-label="label"
:data-state="isActive ? 'active' : 'inactive'"
:class="cn(reactionVariants({ active: isActive }), props.class)"
@click="toggle"
>
<span
v-if="slots.default"
aria-hidden="true"
class="inline-flex shrink-0 items-center"
>
<slot />
</span>
<img
v-else-if="props.emojiSrc"
:src="props.emojiSrc"
alt=""
aria-hidden="true"
:draggable="false"
class="h-4 w-4 object-contain"
/>
<span v-else aria-hidden="true">{{ props.emoji }}</span>
<span aria-hidden="true" class="text-xs font-semibold">{{
props.count
}}</span>
</button>
</template><script setup lang="ts">
import type { HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
const props = defineProps<{ class?: HTMLAttributes["class"] }>()
</script>
<!-- The row of reactions under a message. -->
<template>
<div :class="cn('mt-1 flex flex-wrap items-center gap-1', props.class)">
<slot />
</div>
</template>import { cva, type VariantProps } from "class-variance-authority"
export { default as Reaction } from "./Reaction.vue"
export { default as ReactionGroup } from "./ReactionGroup.vue"
export const reactionVariants = cva(
"inline-flex items-center gap-1 rounded-[var(--newt-radius-md)] border px-2 py-0.5 text-[13px] transition-[background-color,border-color] duration-fast ease-newt focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link",
{
variants: {
active: {
false: "border-newt-border bg-newt-bg-surface text-newt-text-secondary",
true: "border-newt-brand bg-[color-mix(in_srgb,var(--newt-brand)_15%,transparent)] text-newt-mention-text",
},
/* `false` for a read-only reaction: no pointer, no hover. */
interactive: {
true: "cursor-pointer",
false: "cursor-[inherit]",
},
},
compoundVariants: [
{ active: false, interactive: true, class: "hover:bg-newt-bg-hover" },
],
defaultVariants: {
active: false,
interactive: true,
},
}
)
export type ReactionVariants = VariantProps<typeof reactionVariants>
/** What the accessible name of a reaction is built from. */
export interface ReactionLabelState {
/** The emoji's text, or `emojiAlt` for an image emoji, or `"Emoji"`. */
emoji: string
count: number
active: boolean
}
/** The English default. Pass `formatLabel` to translate it. */
export function defaultReactionLabel({
emoji,
count,
active,
}: ReactionLabelState): string {
return `${emoji} reaction, ${count} ${count === 1 ? "person" : "people"}${
active ? ", you reacted" : ""
}`
}Update the import paths
The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Reaction Pill to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Reaction Pill to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
Usage
import { Reaction } from "@/components/ui/reaction"
export function Example() {
return <Reaction />
}<script setup lang="ts">
import { Reaction, ReactionGroup } from "@/components/ui/reaction"
</script>
<template>
<Reaction />
</template>Examples
Controlled
Bind active to own the toggle. The pill never changes count by itself, so add the reader’s reaction to the number you pass.
newt-reaction"use client"
import * as React from "react"
import { Reaction, ReactionGroup } from "@/registry/bases/newt/ui/reaction"
const OTHERS = 12
/* The pill reports the toggle; the count is the caller's to keep in step. */
export default function ReactionControlledDemo() {
const [reacted, setReacted] = React.useState(false)
return (
<div className="flex flex-col items-center gap-2">
<ReactionGroup>
<Reaction
emoji="👍"
count={OTHERS + (reacted ? 1 : 0)}
active={reacted}
onActiveChange={setReacted}
/>
</ReactionGroup>
<p className="m-0 text-xs text-newt-text-muted" aria-live="polite">
{reacted ? "You reacted" : "You have not reacted"}
</p>
</div>
)
}<script setup lang="ts">
import { ref } from "vue"
import { Reaction, ReactionGroup } from "~~/registry/bases/newt/ui/reaction"
const OTHERS = 12
/* The pill reports the toggle; the count is the caller's to keep in step. */
const reacted = ref(false)
</script>
<template>
<div class="flex flex-col items-center gap-2">
<ReactionGroup>
<Reaction
v-model:active="reacted"
emoji="👍"
:count="OTHERS + (reacted ? 1 : 0)"
/>
</ReactionGroup>
<p class="m-0 text-xs text-newt-text-muted" aria-live="polite">
{{ reacted ? "You reacted" : "You have not reacted" }}
</p>
</div>
</template>Custom emoji
emojiSrc draws an image in place of the emoji text, for a server’s own emoji. Give it an emojiAlt so the accessible name says which one it is.
newt-reactionimport { Reaction, ReactionGroup } from "@/registry/bases/newt/ui/reaction"
/* Inline so the demo never reaches out to the network for an emoji. */
const LEAF =
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%2323a55a' d='M4 20c0-9 6-16 16-16 0 10-7 16-16 16Z'/%3E%3C/svg%3E"
/* `emojiSrc` draws the image; `emojiAlt` is what a screen reader calls it. */
export default function ReactionCustomEmojiDemo() {
return (
<ReactionGroup>
<Reaction emojiSrc={LEAF} emojiAlt=":leaf:" count={7} defaultActive />
<Reaction emoji="🔥" count={3} />
</ReactionGroup>
)
}<script setup lang="ts">
import { Reaction, ReactionGroup } from "~~/registry/bases/newt/ui/reaction"
/* Inline so the demo never reaches out to the network for an emoji. */
const LEAF =
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%2323a55a' d='M4 20c0-9 6-16 16-16 0 10-7 16-16 16Z'/%3E%3C/svg%3E"
/* `emojiSrc` draws the image; `emojiAlt` is what a screen reader calls it. */
</script>
<template>
<ReactionGroup>
<Reaction :emoji-src="LEAF" emoji-alt=":leaf:" :count="7" default-active />
<Reaction emoji="🔥" :count="3" />
</ReactionGroup>
</template>Icon emoji
Pass the emoji as children in React, or in the default slot in Vue, to draw it with an icon or an image component. The slot wins over emoji and emojiSrc. Set emojiAlt so the accessible name says which emoji it is.
newt-reactionimport { Reaction, ReactionGroup } from "@/registry/bases/newt/ui/reaction"
/* Stands in for any icon component; inline so the demo stays offline. */
const StarIcon = () => (
<svg viewBox="0 0 24 24" focusable="false" className="h-4 w-4">
<path
fill="currentColor"
d="m12 3 2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9L12 3Z"
/>
</svg>
)
/* The children draw the emoji; `emojiAlt` still names it for a screen reader. */
export default function ReactionIconDemo() {
return (
<ReactionGroup>
<Reaction emojiAlt="star" count={2} defaultActive>
<StarIcon />
</Reaction>
<Reaction emoji="🔥" count={3} />
</ReactionGroup>
)
}<script setup lang="ts">
import { Reaction, ReactionGroup } from "~~/registry/bases/newt/ui/reaction"
</script>
<template>
<!--
The default slot draws the emoji, here an inline icon that stands in for
any icon component. `emoji-alt` still names it for a screen reader.
-->
<ReactionGroup>
<Reaction emoji-alt="star" :count="2" default-active>
<svg viewBox="0 0 24 24" focusable="false" class="h-4 w-4">
<path
fill="currentColor"
d="m12 3 2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9L12 3Z"
/>
</svg>
</Reaction>
<Reaction emoji="🔥" :count="3" />
</ReactionGroup>
</template>Read-only
readonly renders a tally that cannot be toggled or focused. Use it in feeds and previews where the reader cannot react.
newt-reactionimport { Reaction, ReactionGroup } from "@/registry/bases/newt/ui/reaction"
/* `readonly` shows the tally as text: no button, no hover, no tab stop. */
export default function ReactionReadonlyDemo() {
return (
<ReactionGroup>
<Reaction emoji="🎉" count={31} readonly />
<Reaction emoji="❤️" count={8} readonly active />
<Reaction emoji="👀" count={2} readonly />
</ReactionGroup>
)
}<script setup lang="ts">
import { Reaction, ReactionGroup } from "~~/registry/bases/newt/ui/reaction"
/* `readonly` shows the tally as text: no button, no hover, no tab stop. */
</script>
<template>
<ReactionGroup>
<Reaction emoji="🎉" :count="31" readonly />
<Reaction emoji="❤️" :count="8" readonly active />
<Reaction emoji="👀" :count="2" readonly />
</ReactionGroup>
</template>API reference
Reaction
children / default slot?React.ReactNode / Vue slot
The emoji, drawn by an icon or an image component. Wins over `emoji` and `emojiSrc`. Name it with `emojiAlt`.
React.ReactNode / Vue slotemoji?React.ReactNode (React) / string (Vue)
Emoji or icon rendered on the left. Optional with `emojiSrc`.
React.ReactNode (React) / string (Vue)emojiSrc?string | undefined
Image URL of a server (custom) emoji, rendered instead of `emoji`.
string | undefinedemojiAlt?string | undefined
Name of the emoji for the accessible name, such as `wolf`.
string | undefinedcount?number
Number of users who reacted.
numberactive?boolean | undefined
Controlled active state.
boolean | undefineddefaultActive?boolean | undefined
Initial active state when uncontrolled.
boolean | undefinedonActiveChange?(active: boolean) => void | undefined
Called with the next active state when toggled.
(active: boolean) => void | undefinedreadonly?boolean
Show the count without a toggle: a `<span>` that is not focusable and ignores clicks.
booleanfalseformatLabel?(state: { emoji: string; count: number; active: boolean }) => string
Builds the accessible name. The English default reads "wolf reaction, 3 people"; pass a translation here. `aria-label` still wins when both are set.
(state: { emoji: string; count: number; active: boolean }) => stringThe registry exports Reaction, ReactionGroup. Parts without a table accept the native attributes for the element they render; React uses className, while Vue uses class and fallthrough attributes.
Accessibility
The reaction is a real button and publishes its toggled state through aria-pressed. A readonly reaction is an image with the same accessible name instead, so a feed that only shows counts adds no tab stops. Translate the name with formatLabel when the page is not in English. Keep the emoji name and count understandable to screen readers.
Translating labels lists every English default.
Tokens
Reaction Pill reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-reaction.
--newt-radius-md--newt-border--newt-bg-surface--newt-text-secondary--newt-duration-fast--newt-ease--newt-bg-hover--newt-brand--newt-mention-text--newt-text-link