Metabase emotion-migrate
Migrate Emotion styled-components to Mantine components with style props and CSS modules. Use when converting .styled.tsx files or removing @emotion imports from components.
git clone https://github.com/metabase/metabase
T=$(mktemp -d) && git clone --depth=1 https://github.com/metabase/metabase "$T" && mkdir -p ~/.claude/skills && cp -r "$T/.claude/skills/emotion-migrate" ~/.claude/skills/metabase-metabase-emotion-migrate && rm -rf "$T"
.claude/skills/emotion-migrate/SKILL.mdEmotion → Mantine + CSS Modules Migration Skill
Migrate Emotion styled-components (
@emotion/styled, @emotion/react) to Mantine layout components with style props and CSS modules. The goal is zero Emotion imports, zero inline styles, and maximum use of design system tokens.
Priority Order (Strict)
- Mantine components + style props —
,Box
,Flex
,Stack
,Group
,Text
,Title
. This is the DEFAULT. Every CSS property must be checked against style props FIRST.Card - CSS modules (
) — ONLY for properties that Mantine style props genuinely cannot express: pseudo-selectors (.module.css
,:hover
,:focus
),::before
,box-shadow
shorthand,border
/animation
, complex selectors,@keyframes
,cursor
,pointer-events
,overflow
,text-overflow
,white-space
.transition - Inline styles ONLY for dynamic values —
is allowed only for truly dynamic runtime values (e.g., computed widths, positions, data-driven colors). All static styles must use Mantine props or CSS modules.style={{ }}
Mantine-First Decision Gate (CRITICAL)
For EACH styled component, go through every CSS property and ask: "Can this be a Mantine style prop?" If yes → style prop. If no → CSS module. Do NOT dump an entire component into a CSS module just because one property needs it — split them.
Properties that ARE style props (use these, not CSS modules):
→display
propdisplay
→color
prop (c
,c="brand"
)c="text-primary"
→background-color
prop (bg
)bg="background-primary"
→font-size
prop (fz
)fz="md"
→font-weight
prop (fw
)fw="bold"
→line-height
prop (lh
)lh="md"
→text-align
prop (ta
)ta="center"
(all variants) →padding
,p
,px
,py
,pt
,pb
,plpr
(all variants) →margin
,m
,mx
,my
,mt
,mb
,mlmr
→width
,w
→min-width
,miw
→max-widthmaw
→height
,h
→min-height
,mih
→max-heightmah
→flex
prop (flex
,flex="0 0 auto"
)flex={1}
→gap
prop (on Flex/Stack/Group)gap
→align-items
prop (on Flex/Stack/Group)align
→justify-content
prop (on Flex/Stack/Group)justify
→flex-direction
prop (on Flex)direction
→flex-wrap
prop (on Flex)wrap
→position
proppos
→top/right/bottom/left
,top
,right
,bottom
propsleft
→opacity
propopacity
Properties that NEED CSS modules (no style prop equivalent):
,:hover
,:focus
,:active
,::before
(pseudo-selectors)::after
,box-shadow
(shorthand with color),borderoutline
,cursorpointer-events
,overflow
,text-overflowwhite-space
,animation
,transitiontransform
queries (UNLESS it's simple responsive spacing/sizing — then use responsive syntax:@media
)p={{ base: "md", lg: "xl" }}
Hybrid approach — when a component needs BOTH, put style props on the Mantine component AND add a CSS module class for the rest:
<Flex className={S.root} /* for :hover, box-shadow, border */ align="center" /* style prop */ gap="sm" /* style prop */ p="md" /* style prop */ bg="background-primary" /* style prop */ >
CSS Module Conventions (Strict)
Class Naming: camelCase
All CSS module class names MUST use camelCase. This is the dominant convention across the codebase (~830 camelCase vs ~620 PascalCase classes), used consistently in Mantine UI components, and matches standard CSS module conventions.
/* CORRECT */ .root { } .settingsSection { } .dragHandle { } .closeIcon { } /* WRONG — do not use PascalCase or kebab-case */ .ItemRoot { } .settings-section { }
Modifier/state classes also use camelCase:
.selected { } .disabled { } .interactive { } .draggable { }
No Cascading — Direct Class Assignment
Cascading selectors are discouraged. Instead of styling through parent-child relationships, assign a class directly to the element that needs styling.
/* WRONG — cascading/descendant selectors */ .root > input { } .root .label { } .container > div > span { } /* CORRECT — direct class on the target element */ .input { } .label { } .title { }
The only acceptable nesting patterns are:
- Pseudo-selectors on the same element:
.item { &:hover { } } - Modifier composition:
.item { &.selected { } } - Hover-reveal patterns where a parent hover affects a child:
— but only when structurally necessary (the child has no way to know about the parent's hover state).root:hover .showOnHover { opacity: 1; }
Import Alias
Always import the CSS module as
S:
import S from "./ComponentName.module.css";
Step-by-Step Migration Process
Step 1: Read and Understand
Read the
.styled.tsx file AND every component that imports from it. Understand:
- Which styled components are used and where
- Which props drive dynamic styles
- Which styles are static vs conditional
- Which styles can map directly to Mantine style props
Step 2: Classify Each Styled Component
For each styled component, apply the Mantine-First Decision Gate above. Then determine the migration target:
| Emotion Pattern | Migration Target |
|---|---|
with only layout/spacing/color | , , , or with style props. NO CSS module needed. |
with flexbox column | component with style props |
with flexbox row | or component with style props |
/ with color/weight/size | with style props (, , ). NO CSS module. |
with hover/focus/pseudo-selectors | Hybrid: Mantine component with style props for expressible properties + CSS module class for pseudo-selectors only |
with media queries (simple spacing/sizing) | Responsive style props: . NO CSS module. |
with media queries (complex/non-spacing) | CSS module for the media query parts, style props for the rest |
with animations/keyframes | CSS module for animation, style props for layout |
with only color/spacing/flex | Wrap in Mantine component with style props: , or pass style props if the component accepts them |
with pseudo-selectors/complex styles | CSS module on the component |
Dynamic props | Mantine style props for simple toggles (), with CSS module classes for complex state combinations involving pseudo-selectors |
Step 3: Create CSS Module (if needed)
Create
ComponentName.module.css alongside the component file:
/* Use design system tokens — NEVER raw color/spacing values */ .root { border: 1px solid var(--mb-color-border); border-radius: var(--mantine-radius-md); background-color: var(--mb-color-background-primary); } /* Conditional states as separate classes, combined with cx() */ .active { background-color: var(--mb-color-brand); color: var(--mb-color-text-primary-inverse); } .disabled { color: var(--mb-color-text-tertiary); pointer-events: none; } /* Hover/focus/pseudo-selectors — nest with & */ .interactive { cursor: pointer; &:hover { color: var(--mb-color-brand); background-color: var(--mb-color-background-hover); } } /* Hover-reveal: acceptable parent→child nesting */ .root:hover .showOnHover { opacity: 1; } /* Responsive styles */ @media (--breakpoint-min-md) { .root { padding: var(--mantine-spacing-lg); } }
Step 4: Update the Component TSX
import cx from "classnames"; import CS from "metabase/css/core/index.css"; import { Box, Flex, Group, Stack, Text } from "metabase/ui"; import S from "./ComponentName.module.css"; // Dynamic props → cx() with conditional classes <Flex className={cx(S.root, { [S.active]: isActive, [S.disabled]: disabled, })} align="center" gap="sm" p="md" w="100%" >
Step 5: Delete the .styled.tsx
File
.styled.tsxRemove the old styled file entirely. Remove all imports of it from other files.
Step 6: Verify
- Confirm zero
or@emotion/styled
imports remain in the migrated files@emotion/react - Confirm no static inline styles (dynamic runtime values are fine)
- Confirm all colors use tokens, not raw hex/rgb values
Design System Token Reference
Mantine Style Props (use directly on components)
Spacing (
p, px, py, pt, pb, pl, pr, m, mx, my, mt, mb, ml, mr):
= 4px,"xs"
= 8px,"sm"
= 16px,"md"
= 24px,"lg"
= 32px"xl"- Custom:
for non-standard values (importrem(48)
fromrem
)metabase/ui
Dimensions (
w, h, maw, mah, miw, mih):
,"100%"
,"100vh"
, etc.rem(400)
Colors (
c, bg):
- Text:
,"text-primary"
,"text-secondary""text-tertiary" - Background:
,"background-primary"
,"background-secondary""background-hover" - Brand:
,"brand"
,"error"
,"success""warning"
Typography (
fz, fw, lh, ta, ff):
- Font size:
= 11px,"xs"
= 12px,"sm"
= 14px,"md"
= 17px,"lg"
= 21px"xl" - Font weight:
,"bold"
, or numeric"normal"700 - Line height:
= 100%,"xs"
= 115%,"sm"
= 122%,"md"
= 138%,"lg"
= 150%"xl" - Text align:
,"center"
,"left""right"
Flexbox (
align, justify, gap, direction, wrap, flex):
,align="center"
,justify="space-between"
,gap="sm"
,direction="column"wrap="nowrap"
,flex={1}flex="0 0 auto"
Other:
pos (position), display
Responsive syntax:
px={{ base: "md", md: "lg", lg: rem(48) }}
CSS Variable Tokens (use in .module.css
files)
.module.cssColors —
var(--mb-color-<name>):
,--mb-color-text-primary
,--mb-color-text-secondary--mb-color-text-tertiary
,--mb-color-background-primary
,--mb-color-background-secondary--mb-color-background-hover
,--mb-color-border
,--mb-color-border-strong--mb-color-border-subtle
,--mb-color-brand--mb-color-brand-hover
,--mb-color-error
,--mb-color-success--mb-color-warning
,--mb-color-shadow--mb-color-focus
through--mb-color-accent0--mb-color-accent7
Mantine spacing —
var(--mantine-spacing-<size>):
(4px),--mantine-spacing-xs
(8px),--mantine-spacing-sm
(16px),--mantine-spacing-md
(24px),--mantine-spacing-lg
(32px)--mantine-spacing-xl
Mantine radius —
var(--mantine-radius-<size>):
(4px),--mantine-radius-xs
(6px),--mantine-radius-sm
(8px),--mantine-radius-md
(40px)--mantine-radius-xl
Mantine font sizes —
var(--mantine-font-size-<size>):
(11px),--mantine-font-size-xs
(12px),--mantine-font-size-sm
(14px),--mantine-font-size-md
(17px),--mantine-font-size-lg
(21px)--mantine-font-size-xl
Breakpoints (in
.module.css files):
(40em / 640px)@media (--breakpoint-min-sm)
(60em / 960px)@media (--breakpoint-min-md)
(80em / 1280px)@media (--breakpoint-min-lg)
(120em / 1920px)@media (--breakpoint-min-xl)- Also available:
variants--breakpoint-max-*
Snap Hardcoded Literals to Nearest Design Token (CRITICAL)
When migrating, never carry over hardcoded
/rem
values from the original Emotion code. Instead, snap them to the nearest design system token. The original values were often arbitrary — the migration is an opportunity to align with the design system.px
Spacing — snap to the nearest Mantine spacing token:
| Hardcoded value | Nearest token | Style prop | CSS variable |
|---|---|---|---|
(2px) | | (number) | (keep literal, no token) |
(3.2px), (4px) | xs (4px) | | |
(8px) | sm (8px) | | |
(12px) | between sm/md | | (no exact token) |
(16px) | md (16px) | | |
(24px) | lg (24px) | | |
(32px) | xl (32px) | | |
Border radius — snap to the nearest Mantine radius token:
| Hardcoded value | Nearest token | CSS variable |
|---|---|---|
(4px) | xs (4px) | |
(6px) | sm (6px) | |
(8px) | md (8px) | |
+ (16px+) | xl (40px) or keep literal | |
Font sizes — snap to the nearest Mantine font size token:
| Hardcoded value | Nearest token | Style prop | CSS variable |
|---|---|---|---|
(11px) | xs (11px) | | |
(12px) | sm (12px) | | |
(14px), (16px) | md (14px) | | |
(17px) | lg (17px) | | |
(21px) | xl (21px) | | |
Rules:
- If the hardcoded value is within ~2px of a token, use the token
- If it falls exactly between two tokens, prefer the smaller one (tighter is safer)
- If no token is close (e.g.,
spacing), use48px
for style props or the literal value in CSS modulesrem(48) - This applies everywhere: style props, CSS module values, Icon
props, etc.size
Common Migration Patterns
Pattern 1: Static Layout Container → Mantine Component
Before:
// Component.styled.tsx export const Container = styled.div` display: flex; align-items: center; gap: 0.5rem; padding: 1rem; width: 100%; `;
After:
<Flex align="center" gap="sm" p="md" w="100%">
Pattern 2: Vertical Stack → Stack Component
Before:
export const Wrapper = styled.div` display: flex; flex-direction: column; gap: 1.5rem; padding: 2rem; `;
After:
<Stack gap="lg" p="xl">
Pattern 3: Dynamic Props → cx() with CSS Module Classes
Before:
export const Item = styled.div<{ isSelected: boolean; disabled: boolean }>` color: ${(props) => props.disabled ? color("text-tertiary") : color("text-primary")}; background-color: ${(props) => props.isSelected ? color("brand") : "transparent"}; cursor: ${(props) => (props.disabled ? "default" : "pointer")}; &:hover { color: ${(props) => !props.disabled && color("brand")}; } `;
After (CSS module):
/* Item.module.css */ .itemRoot { color: var(--mb-color-text-primary); cursor: pointer; &:hover { color: var(--mb-color-brand); } } .selected { background-color: var(--mb-color-brand); } .disabled { color: var(--mb-color-text-tertiary); cursor: default; &:hover { color: var(--mb-color-text-tertiary); } }
After (TSX):
<Box className={cx(S.itemRoot, { [S.selected]: isSelected, [S.disabled]: disabled, })} >
Pattern 4: Extending a Component → Style Props First, CSS Module Only If Needed
When a styled component wraps another component, first check if the styles can be expressed as style props on a Mantine wrapper or on the component itself. Only use a CSS module when properties genuinely need it.
Before:
export const CardIcon = styled(Icon)` display: block; flex: 0 0 auto; color: var(--mb-color-brand); `; export const CardTitle = styled(Ellipsified)` color: var(--mb-color-text-primary); font-size: 1rem; font-weight: bold; margin-left: 1rem; `;
After — style props on Mantine wrapper (preferred when ALL properties are expressible):
<Box display="block" flex="0 0 auto" c="brand"> <Icon {...icon} /> </Box> <Box c="text-primary" fz="md" fw="bold" ml="md"> <Ellipsified>{title}</Ellipsified> </Box>
After — CSS module (use only when component has pseudo-selectors or non-expressible styles):
.icon { color: var(--mb-color-brand); width: 1.5rem; height: 1.5rem; cursor: pointer; &:hover { color: var(--mb-color-brand-hover); } }
<Icon className={S.icon} name="warning" />
Pattern 4b: styled.span
/ styled.p
with Text Styles → Text
Component
styled.spanstyled.pTextBefore:
export const Label = styled.span` color: var(--mb-color-text-secondary); font-weight: bold; `; export const Title = styled.span` color: var(--mb-color-text-primary); font-size: 1rem; font-weight: bold; margin-left: 0.5rem; `;
After —
component with style props (NO CSS module needed):Text
<Text component="span" c="text-secondary" fw="bold"> {label} </Text> <Text component="span" c="text-primary" fz="md" fw="bold" ml="sm"> {title} </Text>
Text renders as <p> by default. Use component="span" to render inline. All color, typography, and spacing props work directly — no CSS module needed for pure text styling.
Pattern 5: Keyframes Animation → CSS Module
Before:
const spin = keyframes` from { transform: rotate(0deg); } to { transform: rotate(360deg); } `; export const Spinner = styled.div` animation: ${spin} 1s infinite linear; `;
After (CSS module):
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } .spinner { animation: spin 1s infinite linear; }
After (TSX):
<Box className={S.spinner} />
Pattern 6: color() Function → CSS Variables
| Emotion (JS) | CSS Module | Mantine Style Prop |
|---|---|---|
| | or |
| | |
| | N/A (use CSS module) |
| | N/A (use CSS module) |
Pattern 7: Responsive Spacing/Sizing → Responsive Style Props (NOT CSS Media Queries)
When a styled component only changes spacing, sizing, or other style-prop-expressible values at breakpoints, use Mantine's responsive syntax instead of CSS module media queries.
Before:
export const CaptionRoot = styled.div` margin-bottom: 1.5rem; ${breakpointMinExtraLarge} { margin-bottom: 2rem; } `;
After — responsive style prop (NO CSS module needed):
<Flex mb={{ base: "lg", xl: "xl" }}>
Breakpoint keys:
base (default), xs (40em), sm (48em), md (60em), lg (80em), xl (120em).
Only use CSS module
@media queries when the responsive change involves non-style-prop properties (e.g., box-shadow, border, cursor changes at breakpoints).
Pattern 8: Dynamic Computed Styles → Inline Styles
Inline styles are allowed only for truly dynamic values computed at runtime (e.g., widths, positions, colors from data). Everything else must use Mantine style props or CSS modules.
Before:
export const Bar = styled.div<{ width: number }>` width: ${(props) => props.width}%; `;
After:
<Box style={{ width: `${width}%` }} />
Pattern 9: Core CSS Utility Classes (Discouraged)
Do NOT introduce new
utility class usage. Core CSS utilities (CS
CS from metabase/css/core/index.css) are legacy and discouraged for new code. Prefer Mantine style props or CSS modules instead.
If existing code already uses
CS classes and you're not migrating that specific code, leave them alone. But when migrating Emotion → Mantine, replace with the proper alternative:
Instead of | Use |
|---|---|
| CSS module: |
| CSS module: |
| Style prop: |
| Style prop: |
| CSS module: |
Pattern 10: Shared Emotion Styles Across Files → Shared CSS Module + cx()
When Emotion exports a shared
css block (like animationStyles) imported by many files, create ONE CSS module with the shared keyframe and a reusable class, then compose via cx().
Before (Emotion — shared animation used by 13+ skeleton files):
// ChartSkeleton.styled.tsx — defines shared animation const fadingKeyframes = keyframes` 0% { opacity: 0.0625; } 50% { opacity: 0.125; } 100% { opacity: 0.0625; } `; export const animationStyles = css` opacity: 0.1; animation: ${fadingKeyframes} 1.5s infinite; `; // AreaSkeleton.styled.tsx — consumes it import { animationStyles } from ".../ChartSkeleton.styled"; export const SkeletonImage = styled.svg` ${animationStyles}; flex: 1 1 0; margin-top: 1rem; border-bottom: 1px solid currentColor; `; // BarSkeleton.styled.tsx — also consumes it import { animationStyles } from ".../ChartSkeleton.styled"; export const SkeletonImage = styled.svg` ${animationStyles}; flex: 1 1 0; margin-top: 1rem; `;
After — shared CSS module:
/* ChartSkeleton.module.css */ @keyframes fading { 0% { opacity: 0.0625; } 50% { opacity: 0.125; } 100% { opacity: 0.0625; } } .animated { opacity: 0.1; animation: fading 1.5s infinite; }
After — per-component CSS modules with own styles only:
/* AreaSkeleton.module.css */ .skeletonImage { flex: 1 1 0; margin-top: 1rem; border-bottom: 1px solid currentColor; }
/* BarSkeleton.module.css */ .skeletonImage { flex: 1 1 0; margin-top: 1rem; }
After — component TSX uses cx() to compose:
// AreaSkeleton.tsx import cx from "classnames"; import ChartSkeletonS from "../ChartSkeleton/ChartSkeleton.module.css"; import S from "./AreaSkeleton.module.css"; const AreaSkeleton = (): JSX.Element => { return ( <svg className={cx(ChartSkeletonS.animated, S.skeletonImage)} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 371 113" preserveAspectRatio="none" > <path d="..." fill="currentColor" /> </svg> ); };
The shared module owns the
@keyframes and the .animated class. Each consumer composes it with its own module class via cx(). No duplication of the keyframe definition.
Import Template
import cx from "classnames"; // only if needed for conditional classes import { Box, Flex, Group, Stack, Text, rem } from "metabase/ui"; import S from "./ComponentName.module.css"; // only if CSS module is needed
Checklist Before Finishing
- MANTINE-FIRST CHECK: Every CSS property that has a style prop equivalent (
,c
,bg
,p
,m
,fw
,fz
,flex
,gap
,align
,w
, etc.) is expressed as a style prop, NOT in a CSS moduleh -
/styled.span
with only color/weight/size are replaced withstyled.p
+ style props, NOT CSS module classesText component="span" -
with only layout props are replaced withstyled.div
/Flex
/Stack
/Group
+ style props, NOT CSS module classesBox - Responsive spacing/sizing uses responsive style props (
), NOT CSS modulemb={{ base: "lg", xl: "xl" }}
queries@media - CSS modules are used ONLY for: pseudo-selectors, box-shadow, border, cursor, overflow, animation, transition, transform, or complex selectors
- All
and@emotion/styled
imports removed from migrated files@emotion/react - The
file is deleted.styled.tsx - All imports of the deleted styled file are updated
- No static inline styles —
used only for truly dynamic runtime valuesstyle={{ }} - All colors use design tokens (
,c="brand"
), not raw hex/rgbvar(--mb-color-brand) - All spacing uses design tokens (
,p="md"
), not arbitrary px values — hardcodedvar(--mantine-spacing-md)
/rem
literals snapped to the nearest token (see mapping table above)px - All border-radius values use radius tokens (
), not hardcodedvar(--mantine-radius-md)0.5rem - All font sizes use font-size tokens (
,fz="md"
), not hardcodedvar(--mantine-font-size-md)1rem - Layout uses Mantine components (
,Flex
,Stack
,Group
,Box
) not raw divs/spans with CSSText - All CSS module class names use camelCase
- No cascading/descendant selectors targeting raw elements — use direct class assignment instead
- Component renders identically to the original (visually verify if possible)
Visual Verification with Screenshots (Optional — Only When Explicitly Asked)
When the user explicitly asks to create before/after screenshots, use the Playwright MCP tools to capture them. This is NOT done by default — only when requested.
Screenshots must be taken in both light and dark mode to verify token usage is correct in both themes.
Switching Color Scheme via API
Toggle between light and dark mode using the Metabase settings API. Use Playwright's
browser_evaluate or browser_navigate with fetch:
// Switch to dark mode await fetch("/api/setting/color-scheme", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ value: "dark" }), }); // Switch to light mode await fetch("/api/setting/color-scheme", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ value: "light" }), });
After switching, reload the page for the theme to take effect.
Process
For each state (after = migrated code, before = stashed original), capture screenshots in both themes at both viewport widths. This produces 8 screenshots total.
-
Take "after" screenshots first (migrated code is already in place):
- Navigate to the page that renders the migrated components (
)browser_navigate - Wait for content to load (
with key text, orbrowser_wait_for
)textGone: "Loading..." - Close the sidebar if it overlaps content on narrow viewports (
the toggle button)browser_click - Light mode (set
tocolor-scheme
via API, reload):"light"- Desktop (1280x900):
after-light-desktop.png - Narrow (640x900), close sidebar:
after-light-narrow.png
- Desktop (1280x900):
- Dark mode (set
tocolor-scheme
via API, reload):"dark"- Desktop (1280x900):
after-dark-desktop.png - Narrow (640x900), close sidebar:
after-dark-narrow.png
- Desktop (1280x900):
- Navigate to the page that renders the migrated components (
-
Stash changes to capture "before" screenshots:
- Run
to temporarily revert to the Emotion versiongit stash --include-untracked - Repeat the same 4 screenshots with
prefix:before-
,before-light-desktop.pngbefore-light-narrow.png
,before-dark-desktop.pngbefore-dark-narrow.png
- Run
to restore the migrated codegit stash pop
- Run
-
Restore the user's original color scheme (set back to
or whatever it was before)."auto" -
Display all 8 screenshots using the
tool so the user can visually compare before/after in both themes at both viewport sizes.Read
Key Details
- Save screenshots in the repo root (they will be untracked — user can delete after review)
- The dev server must be running (typically
)http://localhost:3000 - Use
to find element refs when you need to click buttons (e.g., sidebar toggle)browser_snapshot - The greeting text on the home page changes randomly on each load — this is expected, not a regression
- If the page being tested is not the home page, navigate to the correct URL that exercises the migrated components
- Dark mode screenshots are critical for verifying that CSS variable tokens (
) are used correctly — hardcoded colors will look wrong in dark modevar(--mb-color-*)