data attributes
Mantine components use data-*
attributes to apply styles. These attributes are used as
a modifier to apply styles based on component state. General rule of Mantine components
styles – one class with shared styles and any number of data-*
attributes as modifiers.
Example of applying styles with data-*
attributes:
data attributes values
Most of the data-*
attributes do not have associated values, they represent boolean
state or feature. For example, when disabled
prop on Button is set
data-disabled
attribute is added to the <button />
element:
Will output the following HTML:
You can then use this attribute to apply styles to disabled button:
When disabled
prop is not set, then data-disabled
attribute is not added to the
button:
In some cases, data-*
attributes have associated values, for example, Button
component section
element has an associated data-position
attribute which can be
left
or right
. The following example will render two section
elements, one with
data-position="left"
and another with data-position="right"
:
Will output the following HTML:
You can then use this attribute to apply styles to the left and right sections:
Components data attributes documentation
Every component that uses data-*
attributes has a dedicated section under Styles API
tab.
Button component data-*
attributes table:
How to read the table:
selector
column – Styles API selector (or multiple selectors) to which data attribute is addedattribute
column – data attribute namecondition
column – condition based on which data attribute is added to the elementvalue
column – value of the data attribute
Box component mod prop
Box component supports mod
prop, which allows adding data attributes to
the element. You can use to create custom components with data attributes.