import * as stylex from "@stylexjs/stylex";
import {
mergeProps,
renderElement,
type PartProps,
} from "@ui-sdk/react/primitive";
import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";
import { defineSlotRecipe, type WithStyleOverrides } from "#lib/recipe-props.ts";
import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
import { zIndices } from '@stylexblocks/ui/tokens/z-indices.stylex.ts'
const bubbleRoot = stylex.create({
base: {
display: 'flex',
position: 'relative',
flexDirection: 'column',
gap: spacing._1,
width: 'fit-content',
maxWidth: '80%',
minWidth: 0,
},
})
const bubbleRootAligns = stylex.create({
start: {
alignSelf: 'flex-start',
},
end: {
alignSelf: 'flex-end',
},
})
const bubbleRootVariants = stylex.create({
ghost: {
maxWidth: '100%',
width: '100%',
},
plain: {
maxWidth: '100%',
width: '100%',
},
})
const bubbleContent = stylex.create({
base: {
minWidth: 0,
overflowWrap: 'anywhere',
borderRadius: semanticRadii.panel,
paddingInline: spacing._3,
paddingBlock: spacing._2,
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
outlineWidth: {
default: null,
':focus-visible': '2px',
},
outlineOffset: {
default: null,
':focus-visible': '2px',
},
outlineStyle: {
default: null,
':focus-visible': 'solid',
},
outlineColor: {
default: null,
':focus-visible': colorPalette.focusRing,
},
cursor: {
default: null,
':is(button, a)': 'pointer',
},
},
})
const bubbleContentVariants = stylex.create({
solid: {
backgroundColor: {
default: colorPalette.solid,
':is(button, a):hover': `color-mix(in oklch, ${colorPalette.solid} 80%, transparent)`,
},
color: colorPalette.contrast,
},
subtle: {
backgroundColor: {
default: colorPalette.subtle,
':is(button, a):hover': colorPalette.muted,
},
color: colorPalette.fg,
},
surface: {
backgroundColor: {
default: colorPalette.muted,
':is(button, a):hover': colorPalette.subtle,
},
color: colorPalette.fg,
boxShadow: 'inset 0 0 0px 1px var(--shadow-color)',
'--shadow-color': colorPalette.border,
},
outline: {
color: colorPalette.fg,
backgroundColor: {
default: 'transparent',
':is(button, a):hover': colorPalette.muted,
},
boxShadow: 'inset 0 0 0px 1px var(--shadow-color)',
'--shadow-color': colorPalette.border,
},
ghost: {
backgroundColor: {
default: 'transparent',
':is(button, a):hover': colorPalette.subtle,
},
color: colorPalette.fg,
paddingInline: 0,
paddingBlock: 0,
borderRadius: 0,
},
plain: {
color: colorPalette.fg,
backgroundColor: 'transparent',
paddingInline: 0,
paddingBlock: 0,
borderRadius: 0,
},
})
const bubbleReactions = stylex.create({
base: {
position: 'absolute',
zIndex: zIndices.docked,
display: 'flex',
width: 'fit-content',
flexShrink: 0,
alignItems: 'center',
justifyContent: 'center',
gap: spacing._1,
borderRadius: radii.full,
backgroundColor: semanticColors.bgMuted,
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
boxShadow: `0 0 0 3px ${semanticColors.bgPanel}`,
paddingInline: {
default: spacing._1_5,
':has(button)': 0,
},
paddingBlock: {
default: spacing._0_5,
':has(button)': 0,
},
},
})
const bubbleReactionsSides = stylex.create({
top: {
top: 0,
transform: 'translateY(-75%)',
},
bottom: {
bottom: 0,
transform: 'translateY(75%)',
},
})
const bubbleReactionsAligns = stylex.create({
start: {
insetInlineStart: spacing._3,
},
end: {
insetInlineEnd: spacing._3,
},
})
const bubbleGroup = stylex.create({
base: {
display: 'flex',
flexDirection: 'column',
gap: spacing._1,
minWidth: 0,
width: '100%',
},
})
export type BubbleVariant = keyof typeof bubbleContentVariants
export type BubbleAlign = keyof typeof bubbleRootAligns
export type BubbleReactionSide = keyof typeof bubbleReactionsSides
export type BubbleReactionAlign = keyof typeof bubbleReactionsAligns
const bubbleSlotRecipe = defineSlotRecipe({
slots: {
root: {
styles: bubbleRoot,
aligns: bubbleRootAligns,
variants: bubbleRootVariants,
},
content: { styles: bubbleContent, variants: bubbleContentVariants },
group: { styles: bubbleGroup },
},
defaultVariants: { variant: "solid", align: "start" },
});
/**
* Bubble — framed conversational surface. `<Bubble.Root variant align>`
* resolves the recipe once and every part inherits it through context;
* `Bubble.Reactions` positions a reaction row on the bubble edge via its
* own `side` / `align`. Plain elements rendered through UI SDK's
* `renderElement`, so each part composes via `render`.
*/
const { withProvider, withContext } = createSlotRecipeContext(bubbleSlotRecipe);
type ReactionsProps = WithStyleOverrides<PartProps<"div">> & {
side?: BubbleReactionSide;
align?: BubbleReactionAlign;
};
function Reactions({
side = "bottom",
align = "end",
palette,
style,
render,
...rest
}: ReactionsProps) {
return renderElement(
"div",
mergeProps(
{
"data-slot": "bubble-reactions",
"data-side": side,
"data-align": align,
...stylex.props(
palette,
bubbleReactions.base,
bubbleReactionsSides[side],
bubbleReactionsAligns[align],
style,
),
},
rest,
),
render,
);
}
Reactions.displayName = "BubbleReactions";
export const Bubble = {
Root: withProvider("div", "root", "BubbleRoot"),
Content: withContext("div", "content", "BubbleContent"),
Reactions,
Group: withProvider("div", "group", "BubbleGroup"),
};