-
Notifications
You must be signed in to change notification settings - Fork 179
fix: align banner and toast icons #726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@cloudflare/kumo": patch | ||
| --- | ||
|
|
||
| Align Banner and Toast status icons to the first line of wrapping message text and prevent the icons from shrinking. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import { | ||
| type HTMLAttributes, | ||
| type ReactElement, | ||
| type ReactNode, | ||
| cloneElement, | ||
| forwardRef, | ||
| isValidElement, | ||
| } from "react"; | ||
|
|
@@ -46,7 +48,7 @@ export const KUMO_BANNER_VARIANTS = { | |
| description: "Default banner size", | ||
| }, | ||
| sm: { | ||
| classes: "items-center gap-2 rounded-md px-3 py-2 text-sm", | ||
| classes: "items-start gap-2 rounded-md px-3 py-2 text-sm", | ||
| description: "Compact banner for dialogs and tight spaces", | ||
| }, | ||
| }, | ||
|
|
@@ -63,28 +65,51 @@ export type KumoBannerSize = keyof typeof KUMO_BANNER_VARIANTS.size; | |
|
|
||
| /** | ||
| * Per-size render-site classes not carried by `bannerVariants` (which only emits | ||
| * the container classes). `row` is the title↔action flex gap, `icon` the icon | ||
| * wrapper height, `description` the description text size, and `action` the size | ||
| * the container classes). `row` is the title↔action flex gap, | ||
| * `description` the description text size, and `action` the size | ||
| * that child `Banner.Action`s inherit via {@link BannerActionContext}. | ||
| */ | ||
| const BANNER_SIZE_PARTS: Record< | ||
| KumoBannerSize, | ||
| { row: string; icon: string; description: string; action: BannerActionSize } | ||
| { row: string; description: string; action: BannerActionSize } | ||
| > = { | ||
| base: { | ||
| row: "gap-3", | ||
| icon: "h-[1.375em]", | ||
| description: "text-sm", | ||
| action: "sm", | ||
| }, | ||
| sm: { | ||
| row: "gap-2", | ||
| icon: "h-[1.25em]", | ||
| description: "text-sm", | ||
| action: "xs", | ||
| }, | ||
| }; | ||
|
|
||
| const renderBannerIcon = (icon: ReactNode, className?: string) => { | ||
| const iconElement = isValidElement(icon) | ||
| ? (icon as ReactElement<{ className?: string }>) | ||
| : null; | ||
| const alignedIcon = iconElement | ||
| ? cloneElement(iconElement, { | ||
| className: cn( | ||
| iconElement.props.className, | ||
| "size-[1em] flex-none leading-snug", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked downstream Stratus usage and this does have compatibility impact: there are at least 123 Is taking ownership of all Banner icon sizing intentional here? If the goal is only alignment/non-shrinking, the outer
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intentional. Custom sizes downstream is a defect (in my opinion), but point heard about the impact. |
||
| ), | ||
| }) | ||
| : icon; | ||
|
|
||
| return ( | ||
| <span | ||
| className={cn( | ||
| "flex h-[1lh] flex-none items-center leading-snug", | ||
| className, | ||
| )} | ||
| > | ||
| {alignedIcon} | ||
| </span> | ||
| ); | ||
| }; | ||
|
|
||
| // The `Banner.Action` CTA compound lives in ./banner-action | ||
| // and is attached to `Banner` via Object.assign at the bottom of this file. | ||
| export type { | ||
|
|
@@ -253,17 +278,7 @@ const BannerRoot = forwardRef<HTMLDivElement, BannerProps>(function BannerRoot( | |
| className={cn(bannerVariants({ variant, size }), className)} | ||
| {...props} | ||
| > | ||
| {icon && ( | ||
| <span | ||
| className={cn( | ||
| "flex shrink-0 items-center", | ||
| sizeParts.icon, | ||
| variantConfig.iconClasses, | ||
| )} | ||
| > | ||
| {icon} | ||
| </span> | ||
| )} | ||
| {icon && renderBannerIcon(icon, variantConfig.iconClasses)} | ||
| <div | ||
| className={cn( | ||
| "flex min-w-0 flex-1 items-center justify-between", | ||
|
|
@@ -328,11 +343,7 @@ const BannerRoot = forwardRef<HTMLDivElement, BannerProps>(function BannerRoot( | |
| className={cn(bannerVariants({ variant, size }), className)} | ||
| {...props} | ||
| > | ||
| {icon && ( | ||
| <span className={cn("shrink-0", variantConfig.iconClasses)}> | ||
| {icon} | ||
| </span> | ||
| )} | ||
| {icon && renderBannerIcon(icon, variantConfig.iconClasses)} | ||
| {content} | ||
| </div> | ||
| </BannerActionContext.Provider> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we shouldn't test on the existence of class names, it's brittle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
truth! I will fix this