Loading...
Loading...
Mantine v7 component library — theming, styling, hooks, forms, notifications
npx skill4agent add agents-inc/skills web-ui-mantineQuick Guide: Mantine v7 is a full-featured React component library using CSS Modules (not CSS-in-JS). Wrap your app inwith aMantineProviderobject. Style with CSS Modules + PostCSS preset mixins, the Styles API (createTheme/classNamesprops), or style props (styles,p,m, etc.). Usebgfor form management,@mantine/formfor utility hooks, and@mantine/hooksfor toasts. PostCSS preset with@mantine/notificationsis required for responsive mixins andpostcss-preset-mantine.light-dark()
All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering,, named constants)import type
MantineProvider@mantine/core/styles.csspostcss-preset-mantine.module.csscreateStylesMantineProvider// app.tsx
import "@mantine/core/styles.css";
// Import additional package styles only if using those packages:
// import '@mantine/dates/styles.css';
// import '@mantine/notifications/styles.css';
import { createTheme, MantineProvider } from "@mantine/core";
const theme = createTheme({
primaryColor: "blue",
defaultRadius: "md",
});
export function App() {
return (
<MantineProvider theme={theme}>{/* Your app content */}</MantineProvider>
);
}// postcss.config.cjs
module.exports = {
plugins: {
"postcss-preset-mantine": {},
"postcss-simple-vars": {
variables: {
"mantine-breakpoint-xs": "36em",
"mantine-breakpoint-sm": "48em",
"mantine-breakpoint-md": "62em",
"mantine-breakpoint-lg": "75em",
"mantine-breakpoint-xl": "88em",
},
},
},
};import { ColorSchemeScript, mantineHtmlProps } from "@mantine/core";
// In your root HTML/layout:
<html lang="en" {...mantineHtmlProps}>
<head>
<ColorSchemeScript defaultColorScheme="auto" />
</head>
<body>
<App />
</body>
</html>;ColorSchemeScriptclassNamestylevariantsizecolorradiusimport {
Button,
TextInput,
Select,
Checkbox,
Group,
Stack,
} from "@mantine/core";
// Button with variants and sizes
<Button variant="filled" color="blue" size="md" loading={isLoading}>
Submit
</Button>
<Button variant="outline">Cancel</Button>
<Button variant="light" color="red" leftSection={<TrashIcon />}>
Delete
</Button>
// Text inputs
<TextInput
label="Email"
placeholder="you@example.com"
withAsterisk
error={errors.email}
/>
// Select
<Select
label="Role"
data={["Admin", "Editor", "Viewer"]}
placeholder="Pick a role"
searchable
clearable
/>
// Checkbox
<Checkbox label="Accept terms" checked={accepted} onChange={handleChange} />
// Layout components
<Group gap="md">{/* Horizontal flex */}</Group>
<Stack gap="sm">{/* Vertical flex */}</Stack>useDisclosureimport { Modal, Button } from "@mantine/core";
import { useDisclosure } from "@mantine/hooks";
function Demo() {
const [opened, { open, close }] = useDisclosure(false);
return (
<>
<Modal opened={opened} onClose={close} title="Confirm Action">
<p>Are you sure?</p>
<Group mt="md" justify="flex-end">
<Button variant="outline" onClick={close}>
Cancel
</Button>
<Button onClick={handleConfirm}>Confirm</Button>
</Group>
</Modal>
<Button onClick={open}>Open Modal</Button>
</>
);
}// Drawer - same pattern as Modal
import { Drawer } from "@mantine/core";
<Drawer opened={opened} onClose={close} title="Settings" position="right">
{/* Drawer content */}
</Drawer>;// Menu
import { Menu, Button } from "@mantine/core";
<Menu shadow="md" width={200}>
<Menu.Target>
<Button>Actions</Button>
</Menu.Target>
<Menu.Dropdown>
<Menu.Label>Application</Menu.Label>
<Menu.Item leftSection={<SettingsIcon />}>Settings</Menu.Item>
<Menu.Divider />
<Menu.Item color="red" leftSection={<TrashIcon />}>
Delete
</Menu.Item>
</Menu.Dropdown>
</Menu>;// Tabs
import { Tabs } from "@mantine/core";
<Tabs defaultValue="general">
<Tabs.List>
<Tabs.Tab value="general">General</Tabs.Tab>
<Tabs.Tab value="security">Security</Tabs.Tab>
</Tabs.List>
<Tabs.Panel value="general">General settings...</Tabs.Panel>
<Tabs.Panel value="security">Security settings...</Tabs.Panel>
</Tabs>;// Accordion
import { Accordion } from "@mantine/core";
<Accordion>
<Accordion.Item value="faq-1">
<Accordion.Control>How does billing work?</Accordion.Control>
<Accordion.Panel>Monthly billing starts on signup date.</Accordion.Panel>
</Accordion.Item>
<Accordion.Item value="faq-2">
<Accordion.Control>Can I cancel anytime?</Accordion.Control>
<Accordion.Panel>Yes, cancel anytime from settings.</Accordion.Panel>
</Accordion.Item>
</Accordion>;useDisclosurelight-dark()/* feature-card.module.css */
.card {
padding: var(--mantine-spacing-md);
border-radius: var(--mantine-radius-md);
background-color: light-dark(
var(--mantine-color-white),
var(--mantine-color-dark-6)
);
@mixin hover {
box-shadow: var(--mantine-shadow-md);
}
@mixin smaller-than 48em {
padding: var(--mantine-spacing-sm);
}
}
.title {
font-weight: 700;
color: light-dark(var(--mantine-color-black), var(--mantine-color-white));
}import { Card, Text } from "@mantine/core";
import classes from "./feature-card.module.css";
export function FeatureCard({ title, children }: FeatureCardProps) {
return (
<Card className={classes.card}>
<Text className={classes.title}>{title}</Text>
{children}
</Card>
);
}// Target specific inner elements with classNames
<TextInput
classNames={{
root: classes.inputRoot,
input: classes.inputField,
label: classes.inputLabel,
}}
/>
// Or inline styles for quick overrides
<TextInput
styles={{
input: { backgroundColor: "var(--mantine-color-blue-0)" },
label: { fontWeight: 700 },
}}
/>import { Box } from "@mantine/core";
<Box
p="md"
bg="blue.1"
c="blue.9"
w={{ base: "100%", sm: 400, lg: 600 }}
fz="sm"
ta="center"
/>;import {
createTheme,
MantineProvider,
virtualColor,
Button,
} from "@mantine/core";
const BRAND_COLORS = [
"#f0e4ff",
"#d9b8ff",
"#c28dff",
"#aa61ff",
"#9336ff",
"#7b0bff",
"#6700d9",
"#5300b3",
"#3f008c",
"#2b0066",
] as const;
const theme = createTheme({
// Colors - each color is a 10-shade tuple (index 0 lightest, 9 darkest)
colors: {
brand: [...BRAND_COLORS],
// virtualColor switches based on color scheme
surface: virtualColor({
name: "surface",
dark: "dark",
light: "gray",
}),
},
primaryColor: "brand",
primaryShade: { light: 6, dark: 8 },
// Typography
fontFamily: "Inter, sans-serif",
headings: { fontFamily: "Inter, sans-serif" },
// Layout
defaultRadius: "md",
// Component defaults - override props and styles globally
components: {
Button: Button.extend({
defaultProps: {
variant: "filled",
size: "sm",
},
}),
},
});virtualColorlightdarkautoimport {
useMantineColorScheme,
useComputedColorScheme,
ActionIcon,
} from "@mantine/core";
export function ColorSchemeToggle() {
const { setColorScheme } = useMantineColorScheme();
const computedScheme = useComputedColorScheme("light");
return (
<ActionIcon
variant="outline"
onClick={() =>
setColorScheme(computedScheme === "dark" ? "light" : "dark")
}
>
{computedScheme === "dark" ? <SunIcon /> : <MoonIcon />}
</ActionIcon>
);
}useComputedColorSchemeuseMantineColorScheme().colorScheme"auto"colorScheme"auto"// MantineProvider configuration for color scheme
<MantineProvider theme={theme} defaultColorScheme="auto">
{/* App */}
</MantineProvider>// Built-in props for scheme-conditional rendering
<Button lightHidden>Only visible in dark mode</Button>
<Button darkHidden>Only visible in light mode</Button>autouseComputedColorSchemeautoimport { useForm } from "@mantine/form";
import { TextInput, Checkbox, Button, Group, Box } from "@mantine/core";
export function SignupForm() {
const form = useForm({
mode: "uncontrolled",
initialValues: {
email: "",
name: "",
termsAccepted: false,
},
validate: {
email: (value) => (/^\S+@\S+$/.test(value) ? null : "Invalid email"),
name: (value) =>
value.length < 2 ? "Name must be at least 2 characters" : null,
termsAccepted: (value) => (value ? null : "You must accept terms"),
},
});
return (
<Box component="form" onSubmit={form.onSubmit(handleSubmit)}>
<TextInput
label="Name"
withAsterisk
key={form.key("name")}
{...form.getInputProps("name")}
/>
<TextInput
label="Email"
withAsterisk
key={form.key("email")}
{...form.getInputProps("email")}
/>
<Checkbox
label="I accept the terms"
key={form.key("termsAccepted")}
{...form.getInputProps("termsAccepted", { type: "checkbox" })}
/>
<Group mt="md" justify="flex-end">
<Button type="submit">Submit</Button>
</Group>
</Box>
);
}const form = useForm({
mode: "uncontrolled",
initialValues: { password: "", confirmPassword: "" },
validate: {
confirmPassword: (value, values) =>
value !== values.password ? "Passwords do not match" : null,
},
});const form = useForm({
mode: "uncontrolled",
initialValues: {
employees: [{ name: "", email: "" }],
},
});
// Add item
form.insertListItem("employees", { name: "", email: "" });
// Remove item
form.removeListItem("employees", index);
// Access nested fields with dot notation
form.getInputProps("employees.0.name");
form.getInputProps("employees.0.email");getInputPropsmode: "uncontrolled"form.key()import { useState } from "react";
import {
useDisclosure,
useDebouncedValue,
useMediaQuery,
useClickOutside,
useClipboard,
} from "@mantine/hooks";
// useDisclosure - boolean state with handlers
const [opened, { open, close, toggle }] = useDisclosure(false);
// useDebouncedValue - debounce any value
const [search, setSearch] = useState("");
const [debounced] = useDebouncedValue(search, 300);
// useMediaQuery - responsive logic in JS
const isMobile = useMediaQuery("(max-width: 48em)");
// useClickOutside - close on outside click
const ref = useClickOutside(() => close());
// useClipboard - copy to clipboard with timeout
const clipboard = useClipboard({ timeout: 2000 });
clipboard.copy("text to copy");
// clipboard.copied is true for 2000ms after copyuseDebouncedValueuseMediaQueryundefined// Root setup (once)
import { Notifications } from "@mantine/notifications";
import "@mantine/notifications/styles.css";
<MantineProvider>
<Notifications position="top-right" />
<App />
</MantineProvider>;// Show notifications anywhere
import { notifications } from "@mantine/notifications";
// Basic notification
notifications.show({
title: "Success",
message: "Your changes have been saved",
color: "green",
});
// Loading notification that updates
const id = notifications.show({
loading: true,
title: "Uploading file",
message: "Please wait...",
autoClose: false,
withCloseButton: false,
});
// Update to success
notifications.update({
id,
loading: false,
title: "Upload complete",
message: "File has been uploaded",
color: "green",
autoClose: 3000,
});
// Clean all
notifications.clean();updatenpm install @mantine/dates dayjsimport "@mantine/dates/styles.css";
import { DatePickerInput, DatesProvider } from "@mantine/dates";
// Wrap with DatesProvider for locale/firstDayOfWeek
<DatesProvider settings={{ locale: "en", firstDayOfWeek: 0 }}>
<App />
</DatesProvider>;
// v7: Date objects
const [date, setDate] = useState<Date | null>(null);
// v8: string values in "YYYY-MM-DD" format
// const [date, setDate] = useState<string | null>(null);
<DatePickerInput
label="Pick date"
placeholder="Pick date"
value={date}
onChange={setDate}
/>;
// v7: Date range
const [range, setRange] = useState<[Date | null, Date | null]>([null, null]);
// v8: const [range, setRange] = useState<[string | null, string | null]>([null, null]);
<DatePickerInput
type="range"
label="Date range"
value={range}
onChange={setRange}
/>;</patterns>v8 note: Mantine v8 changed allcomponents from@mantine/datesobjects toDatestrings."YYYY-MM-DD"no longer supports theDatesProvideroption.timezone
createStyles.module.css@mixin smaller-thanlight-dark()rem()@mantine/core/styles.csscolorScheme"auto"useComputedColorScheme"light""dark"form.key()mode: "uncontrolled"getInputProps@mantine/dates/styles.css@mantine/notifications/styles.cssstyleclassNamesstylesmantineHtmlProps<html>ColorSchemeScript<head>p="md"theme.spacing.md"md"color="blue.6"useMediaQueryundefinedtheme.breakpointspostcss.config.cjs@mantine/dates"YYYY-MM-DD"DateDatesProvidertimezoneCodeHighlightlight-dark()postcss-preset-mantineAll code must follow project conventions in CLAUDE.md
MantineProvider@mantine/core/styles.csspostcss-preset-mantine.module.csscreateStyles