import type { ComponentProps } from "react";
import * as stylex from "@stylexjs/stylex";
import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";
import { defineSlotRecipe } from "#lib/recipe-props.ts";
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
const messageRoot = stylex.create({
base: {
display: 'flex',
alignItems: 'flex-start',
gap: spacing._2_5,
width: '100%',
minWidth: 0,
'--message-avatar-nudge': {
default: '0px',
':has([data-slot="message-header"])': `calc(${lineHeights.xs} + ${spacing._1})`,
},
},
})
const messageRootAligns = stylex.create({
start: {
flexDirection: 'row',
justifyContent: 'flex-start',
},
end: {
flexDirection: 'row-reverse',
justifyContent: 'flex-start',
},
})
const messageAvatar = stylex.create({
base: {
display: 'flex',
flexShrink: 0,
alignItems: 'flex-end',
alignSelf: 'flex-start',
marginBlockStart: 'var(--message-avatar-nudge)',
minWidth: sizes._6,
minHeight: sizes._6,
},
})
const messageAvatarAligns = stylex.create({
start: {
justifyContent: 'flex-end',
},
end: {
justifyContent: 'flex-start',
},
})
const messageContent = stylex.create({
base: {
display: 'flex',
flexDirection: 'column',
gap: spacing._1,
minWidth: 0,
flexGrow: 1,
},
})
const messageContentAligns = stylex.create({
start: {
alignItems: 'flex-start',
},
end: {
alignItems: 'flex-end',
},
})
const messageHeader = stylex.create({
base: {
alignSelf: 'flex-start',
color: semanticColors.fgMuted,
fontSize: fontSizes.xs,
fontWeight: fontWeights.medium,
lineHeight: lineHeights.xs,
},
})
const messageFooter = stylex.create({
base: {
display: 'flex',
alignItems: 'center',
gap: spacing._1,
color: semanticColors.fgSubtle,
fontSize: fontSizes.xs,
lineHeight: lineHeights.xs,
},
})
const messageFooterAligns = stylex.create({
start: {
alignSelf: 'flex-start',
justifyContent: 'flex-start',
},
end: {
alignSelf: 'flex-end',
justifyContent: 'flex-end',
},
})
const messageGroup = stylex.create({
base: {
display: 'flex',
flexDirection: 'column',
gap: spacing._2,
minWidth: 0,
width: '100%',
},
})
export type MessageAlign = keyof typeof messageRootAligns
const messageSlotRecipe = defineSlotRecipe({
slots: {
root: { styles: messageRoot, aligns: messageRootAligns },
avatar: { styles: messageAvatar, aligns: messageAvatarAligns },
content: { styles: messageContent, aligns: messageContentAligns },
header: { styles: messageHeader },
footer: { styles: messageFooter, aligns: messageFooterAligns },
group: { styles: messageGroup },
},
defaultVariants: { align: "start" },
});
/**
* Message — conversation row. `<Message.Root align>` resolves alignment
* once and every part inherits it through context; the avatar sits on the
* first bubble, the header stays start-aligned, and the footer follows
* the message side. Each part composes via `render`.
*/
const { withProvider, withContext } = createSlotRecipeContext(messageSlotRecipe);
const HeaderImpl = withContext("div", "header", "MessageHeader");
const FooterImpl = withContext("div", "footer", "MessageFooter");
function Header(props: ComponentProps<typeof HeaderImpl>) {
return <HeaderImpl data-slot="message-header" {...props} />;
}
function Footer(props: ComponentProps<typeof FooterImpl>) {
return <FooterImpl data-slot="message-footer" {...props} />;
}
export const Message = {
Root: withProvider("div", "root", "MessageRoot"),
Avatar: withContext("div", "avatar", "MessageAvatar"),
Content: withContext("div", "content", "MessageContent"),
Header,
Footer,
Group: withProvider("div", "group", "MessageGroup"),
};