- Internal Helpers
- State Variants
- Group/Peer Variants
- Theme/Motion/Contrast Variants
- Pseudo-element Variants
- Responsive Variants
- Structural Pseudo-class Variants
- Form State Variants
- ARIA/Peer/Data Variants
- Group Structural Variants
- Peer Structural Variants
- Group State Variants
- Peer State Variants
- Pseudo-element Variants
- Directionality Variants
- Media Variants
- Prose Element Variants
- Validation
- Parsing and Pretty-printing
- Container Query Helpers
- ARIA Helpers
- Data Helpers
- Variant Ordering
package tw
Module Tw.ModifiersSource
Tailwind variant helpers (hover, focus, dark, responsive, group/peer, etc.)
Internal Helpers
compact_length l renders a CSS length without spaces (for use in class names).
nest_selector ~parent template reads template as a selector and replaces each & in it with parent, as CSS nesting does. A template with no & at all compounds onto parent instead, with a type selector wrapped in :is() since it cannot follow a class. Substituting over the parsed selector rather than over the source text leaves an & inside a quoted attribute value alone.
to_selector modifier base_class generates the CSS selector for a modifier applied to a base class.
is_hover m returns true if the modifier generates a :hover rule.
involves_hover m is whether m's rendering gates on \@media (hover: hover): the hover state itself and its group, peer, named, ancestor and has- forms.
bracket_media_condition inner is the media query a bracket at-rule inner names when it is an @media one, @media_print or @media(width>=600px), with underscores read as spaces, and None when it is not or the query does not parse.
bracket_supports_condition content is the condition a bracket @supports at-rule names under not-, @supports(display:grid) or @supports_not_(display:grid), with underscores read as spaces, and None when it is not one, when it does not parse, or when it is an and/or compound with no single negation.
normalize_supports_condition cond builds the supports-[...] bracket content as a typed CSS @supports condition: underscores become spaces, and a bare property or custom property expands to a (prop: var(--tw)) test, built directly through Css.Supports.property rather than assembled as a string and re-parsed. An already-parenthesised condition or a function call is the text the author wrote, so it is parsed with Css.Supports.of_string instead. The modifier parser validates the result, so only conditions that parse reach a rule.
State Variants
focus_within styles applies styles on :focus-within.
focus_visible styles applies styles on :focus-visible.
Group/Peer Variants
group_hover styles applies styles when .group:hover matches.
group_focus styles applies styles when .group:focus matches.
peer_hover styles applies styles when a preceding .peer:hover matches.
peer_focus styles applies styles when a preceding .peer:focus matches.
group_hocus styles applies styles when .group:hover or .group:focus.
peer_hocus styles applies styles when .peer:hover or .peer:focus.
has selector styles applies styles with :has(selector).
group_has ?name selector styles applies styles with .group:has(selector). Optional name for named groups.
peer_has ?name selector styles applies styles with .peer:has(selector). Optional name for named peers.
Theme/Motion/Contrast Variants
motion_safe styles applies styles when reduced motion is not requested.
motion_reduce styles applies styles when reduced motion is requested.
contrast_more styles applies styles when higher contrast is preferred.
contrast_less styles applies styles when lower contrast is preferred.
starting styles applies styles using @starting-style.
Pseudo-element Variants
Responsive Variants
max_sm styles applies styles below the small breakpoint.
max_md styles applies styles below the medium breakpoint.
max_lg styles applies styles below the large breakpoint.
max_xl styles applies styles below the extra-large breakpoint.
max_xl2 styles applies styles below the 2xl breakpoint.
min_arbitrary px styles applies styles at min-width:pxpx.
max_arbitrary px styles applies styles below pxpx.
Structural Pseudo-class Variants
first_of_type styles applies styles on :first-of-type.
last_of_type styles applies styles on :last-of-type.
only_of_type styles applies styles on :only-of-type.
nth expr styles applies styles on :nth-child(expr).
nth_last expr styles applies styles on :nth-last-child(expr).
Form State Variants
indeterminate styles applies styles on :indeterminate.
out_of_range styles applies styles on :out-of-range.
placeholder_shown styles applies styles on :placeholder-shown.
open_ styles applies styles on :is(:popover-open, open).
user_invalid styles applies styles on :user-invalid.
ARIA/Peer/Data Variants
peer_checked styles applies styles when a preceding .peer:checked matches.
aria_checked styles applies styles when aria-checked=true.
aria_expanded styles applies styles when aria-expanded=true.
aria_selected styles applies styles when aria-selected=true.
aria_disabled styles applies styles when aria-disabled=true.
data_state value style applies style when data-state=value.
data_variant value style applies style when data-variant=value.
data_custom key value style applies style when data-key=value.
data_active styles applies styles when data-active is present.
data_inactive styles applies styles when data-inactive is present.
Group Structural Variants
group_first styles applies styles when in a group-first context.
group_last styles applies styles when in a group-last context.
group_only styles applies styles when in a group-only context.
group_odd styles applies styles when in a group-odd context.
group_even styles applies styles when in a group-even context.
group_first_of_type styles applies styles when in a group-first-of-type context.
group_last_of_type styles applies styles when in a group-last-of-type context.
group_only_of_type styles applies styles when in a group-only-of-type context.
Peer Structural Variants
peer_first styles applies styles when a peer is first.
peer_last styles applies styles when a peer is last.
peer_only styles applies styles when a peer is only.
peer_even styles applies styles when a peer is even.
peer_first_of_type styles applies styles when a peer is first-of-type.
peer_last_of_type styles applies styles when a peer is last-of-type.
peer_only_of_type styles applies styles when a peer is only-of-type.
Group State Variants
group_active styles applies styles when the group is active.
group_visited styles applies styles when the group is visited.
group_disabled styles applies styles when the group is disabled.
group_checked styles applies styles when the group is checked.
group_empty styles applies styles when the group is empty.
group_required styles applies styles when the group is required.
group_valid styles applies styles when the group is valid.
group_invalid styles applies styles when the group is invalid.
group_indeterminate styles applies styles when the group is indeterminate.
group_default styles applies styles when the group is default.
group_open styles applies styles when the group is open.
group_target styles applies styles when the group is the target.
group_optional styles applies styles when the group is optional.
group_read_only styles applies styles when the group is read-only.
group_read_write styles applies styles when the group is read-write.
group_inert styles applies styles when the group is inert.
group_user_valid styles applies styles when the group is user-valid.
group_user_invalid styles applies styles when the group is user-invalid.
group_placeholder_shown styles applies styles when the group placeholder is shown.
group_autofill styles applies styles when the group is autofilled.
group_in_range styles applies styles when the group is in range.
group_out_of_range styles applies styles when the group is out of range.
group_focus_within styles applies styles when focus is within the group.
group_focus_visible styles applies styles when group focus is visible.
group_enabled styles applies styles when the group is enabled.
Peer State Variants
peer_active styles applies styles when a peer is active.
peer_visited styles applies styles when a peer is visited.
peer_disabled styles applies styles when a peer is disabled.
peer_empty styles applies styles when a peer is empty.
peer_required styles applies styles when a peer is required.
peer_valid styles applies styles when a peer is valid.
peer_invalid styles applies styles when a peer is invalid.
peer_indeterminate styles applies styles when a peer is indeterminate.
peer_default styles applies styles when a peer is default.
peer_open styles applies styles when a peer is open.
peer_target styles applies styles when a peer is the target.
peer_optional styles applies styles when a peer is optional.
peer_read_only styles applies styles when a peer is read-only.
peer_read_write styles applies styles when a peer is read-write.
peer_inert styles applies styles when a peer is inert.
peer_user_valid styles applies styles when a peer is user-valid.
peer_user_invalid styles applies styles when a peer is user-invalid.
peer_placeholder_shown styles applies styles when a peer placeholder is shown.
peer_autofill styles applies styles when a peer is autofilled.
peer_in_range styles applies styles when a peer is in range.
peer_out_of_range styles applies styles when a peer is out of range.
peer_focus_within styles applies styles when focus is within a peer.
peer_focus_visible styles applies styles when peer focus is visible.
peer_enabled styles applies styles when a peer is enabled.
Pseudo-element Variants
marker styles applies styles to the ::marker pseudo-element.
selection styles applies styles to the ::selection pseudo-element.
placeholder styles applies styles to the ::placeholder pseudo-element.
backdrop styles applies styles to the ::backdrop pseudo-element.
file styles applies styles to the ::file-selector-button pseudo-element.
first_letter styles applies styles to the ::first-letter pseudo-element.
first_line styles applies styles to the ::first-line pseudo-element.
details_content styles applies styles to the ::details-content pseudo-element.
descendants styles applies styles to all descendants.
Directionality Variants
Media Variants
portrait styles applies styles in portrait orientation.
landscape styles applies styles in landscape orientation.
forced_colors styles applies styles when forced colors are active.
supports condition styles applies styles when the CSS condition is supported.
Prose Element Variants
prose_headings styles applies styles to h1-h6, th within prose.
prose_p styles applies styles to p elements within prose.
prose_a styles applies styles to a elements within prose.
prose_strong styles applies styles to strong elements within prose.
prose_em styles applies styles to em elements within prose.
prose_code styles applies styles to code elements within prose.
prose_pre styles applies styles to pre elements within prose.
prose_ol styles applies styles to ol elements within prose.
prose_ul styles applies styles to ul elements within prose.
prose_li styles applies styles to li elements within prose.
prose_blockquote styles applies styles to blockquote elements within prose.
prose_h1 styles applies styles to h1 elements within prose.
prose_h2 styles applies styles to h2 elements within prose.
prose_h3 styles applies styles to h3 elements within prose.
prose_h4 styles applies styles to h4 elements within prose.
prose_h5 styles applies styles to h5 elements within prose.
prose_h6 styles applies styles to h6 elements within prose.
prose_dl styles applies styles to dl elements within prose.
prose_dt styles applies styles to dt elements within prose.
prose_dd styles applies styles to dd elements within prose.
prose_table styles applies styles to table elements within prose.
prose_tr styles applies styles to tr elements within prose.
prose_picture styles applies styles to picture elements within prose.
prose_img styles applies styles to img elements within prose.
prose_video styles applies styles to video elements within prose.
prose_figure styles applies styles to figure elements within prose.
prose_figcaption styles applies styles to figcaption elements within prose.
prose_hr styles applies styles to hr elements within prose.
prose_th styles applies styles to th elements within prose.
prose_td styles applies styles to td elements within prose.
prose_thead styles applies styles to thead elements within prose.
prose_kbd styles applies styles to kbd elements within prose.
prose_lead styles applies styles to lead elements within prose.
Validation
has_responsive_modifier style checks if a style already has a responsive modifier.
validate_no_nested_responsive styles ensures no responsive modifiers are nested. Raises Failure if validation fails.
Parsing and Pretty-printing
of_string class_str parses a class string with modifiers like "hover:bg-blue-500" and returns (modifiers, base_class).
pp_modifier m returns the string prefix for a modifier (e.g., "hover" for Hover).
apply ?theme modifiers style applies a list of modifier strings to a base style, reading the custom breakpoints and \@custom-variants from theme. Returns None if any modifier is unrecognized. Example: apply ["hover"; "sm"] (bg blue 500) creates a hover:sm:bg-blue-500 style.
Container Query Helpers
negate_container c is c negated, a double negation cancelled and the negation pushed inside a named container: name (C) is name (not C).
container_query_of_token ?theme token reads the container query one modifier token spells, \@sm, \@max-md, \@min-[theme(--breakpoint-lg)] or \@lg/main; None for any other token. A size the project's \@theme declared as --container-<name> is read through theme (default Scheme.default).
container_query_theme_decls q is the theme-layer bindings q read through theme(), e.g. --breakpoint-lg: 64rem for \@min-[theme(--breakpoint-lg)]. Resolving the reference into the query does not consume the token: Tailwind writes its binding into \@layer theme as well, so a consumer reading the token off the sheet still finds it. Empty for a query naming no token.
ARIA Helpers
is_aria_shorthand name returns true if name is a known ARIA shorthand attribute (e.g., "checked", "expanded", "hidden") that maps to aria-X=true.
Data Helpers
val parse_data_expr :
string ->
string
* Cascade.Css.Selector.attribute_match
* Cascade.Css.Selector.attr_flag optionparse_data_expr expr reads a data-[expr] bracket body (the interior text, no brackets) into the attribute name ("data-" ^ name), the match operator, and an optional case-sensitivity flag. Handles $=, ^=, *=, ~=, |= and bare =; underscores in the attribute name and value stand for spaces, per Tailwind's arbitrary-value convention. Reads the decoded expression through Css.Selector.of_string, so it raises Invalid_argument or Cascade.Cursor.Parse_error on a malformed expression; callers parsing untrusted class strings validate first and only call this once that validation has already succeeded. Used both to build the selector at render time and, by that earlier validation, to reject a malformed expression at parse time.
Variant Ordering
The two functions below read one table of cascade positions, so they cannot put a variant in one place as a not-* inner and another place as a class-name token. Both return a key on the same scale, comparable with each other.
variant_order_of_prefix ?theme prefix returns the position of a modifier prefix string in the Tailwind v4 variant cascade. The prefix is one modifier of a class name, the part between two ":" (e.g. "hover", "group-has-checked", "@min-[64rem]"). When theme is supplied, its exact custom-variant names take precedence over the built-in prefix grammar; re-registering "dark" retains its pre-existing built-in position. Returns 0 for a token that names no variant, which Sort.compare_indexed_rules reads as "this rule carries no variant".
variant_inner_order token returns what separates token from another token in the same cascade position: the state a group-/peer- or in- token names on another element, and the variant a not- token negates, all on the scale variant_order_of_prefix returns. 0 for every other token.
variant_inner_token token is the variant immediately wrapped by a group-, peer-, not-, in-, or has- compound token.
variant_order_of_media_cond cond returns the same sort key as variant_order_of_prefix for the corresponding CSS media condition. Used to derive the cascade position of rules whose ordering comes from a nested media query rather than from the class-name prefix alone.
- Internal Helpers
- State Variants
- Group/Peer Variants
- Theme/Motion/Contrast Variants
- Pseudo-element Variants
- Responsive Variants
- Structural Pseudo-class Variants
- Form State Variants
- ARIA/Peer/Data Variants
- Group Structural Variants
- Peer Structural Variants
- Group State Variants
- Peer State Variants
- Pseudo-element Variants
- Directionality Variants
- Media Variants
- Prose Element Variants
- Validation
- Parsing and Pretty-printing
- Container Query Helpers
- ARIA Helpers
- Data Helpers
- Variant Ordering