Skip to content

Lint the component layout rules #175

Backlog

Description

@DetweilerRyan

Why

docs/engineering/component-layout.md (#169) says a component owns its look, its padding and its root's whole display value. The caller owns the component's margin, position, width against the parent, grid or flex item placement, and visibility, hiding only with hidden.

Nothing checks those rules yet. Component tests mustn't assert on CSS classes, as component and hook testing says, and a visual diff would catch drift without saying which rule broke. A lint rule could check them where the classes are written.

Proposal

A local oxlint JS plugin rule, beside the existing ones in scripts/lint/, that reports:

  1. On a component's root element: a class in a layout group the caller owns:
    • margin (m-*, mx-auto);
    • position (static, relative, absolute, fixed, sticky, inset-*, top-* and the like);
    • width or height against the parent (w-*, max-w-*, min-w-*, h-*);
    • grid or flex item placement (col-*, row-*, self-*, justify-self-*, order-*, grow, shrink, basis-*, flex-1).
  2. On a root that takes className: a className not merged into the root last through mergeClasses.
  3. At a call site: a className passed to a component, or to a slot's render function, that contains a display value other than hidden and its responsive forms, such as flex, block or grid.

Classify classes with tailwind-merge's own class groups, from its default config, so the lint and the merge agree on what counts as margin or display.

Decisions to make

  • What counts as a component's root: the outermost JSX element a component function returns. Fragments, conditional returns and null need a decision.
  • Which components it applies to: every component under app/ and src/components/, or only those that take className. Proposed: those that take className, plus any root that carries a caller-owned class.
  • Overlays: dialogs and toasts place themselves. Exempt them by an allow comment with a reason, or by a config list. Proposed: a line-level disable with a reason, as other rules do.
  • Dynamic classes: the rule can check only string literals and template literals' static parts. Proposed: skip classes it can't read, and say so in its README.
  • Its contract: like the other scripts/lint/ rules, it needs a README and tests. Whether it also needs feature scenarios is tooling testing's call.

Timing

After #169 merges, since it adds the rules and mergeClasses. #172, the move to Tailwind v4, may change tailwind-merge's groups, so build against whichever version is current then.

🤖 Generated with Claude Code

Activity

  1. added
    enhancement
    on Nov 27, 2026
  2. DetweilerRyan commented on Dec 1, 2026

    @DetweilerRyan
    OwnerAuthor

    Research on the decisions to make

    Web research and a check against main for each open decision. Each item ends with a recommendation for the owner to accept or reject.

    The layout rules live in Components § Layout, not in docs/engineering/component-layout.md, which doesn't exist.

    Prior art

    None checks both sides, root and call site, so a local rule is justified.

    What counts as a component's root

    • Recommend: check every return of a component function. Look through ?:, && and parentheses, and skip null, strings and numbers.
    • Recommend: treat each child element of a returned fragment as a root, since each sits in the caller's layout.
    • Recommend: report a component that takes className but returns a fragment, as check 2, since it can't merge className.
    • Avoid code-path analysis. It's alpha in oxlint JS plugins, and oxc#18555 reports errors in onCodePathStart.
    • Detect components by name. A capitalized function in a .tsx file is enough. eslint-plugin-react's detection shows that deeper heuristics stay imperfect.

    Which components it applies to

    • The proposed scope is every component. "Takes className, plus any root with a caller-owned class" reports the same roots as checking every root. State it that way.
    • Next.js route files have no caller. app/error.tsx's root has mx-auto max-w-xl, and the rule as proposed reports it.
    • Recommend: exempt route files by name: page, layout, error, not-found, loading, template and global-error.

    Overlays

    • Only Toast is affected today. Its root has fixed bottom-4 right-4.
    • ModalDialog isn't affected. It places itself through the modal-card CSS class and the native <dialog>, which the rule can't see.
    • Recommend: a line-level disable with a reason, as proposed. A config list, like react/forbid-component-props' allowedFor, costs more than one exemption saves.
    • Nothing enforces the reason. oxlint has no rule like eslint-comments/require-description. app/layout.tsx:22 and src/components/RoomEntry.tsx:37 disable rules without one.

    Dynamic classes

    • Skipping is safe. Tailwind only finds complete class names, so a working class is always written whole somewhere.
    • Recommend: read the string arguments of mergeClasses, and both branches of ?: and &&. Every dynamic className on main is one of these, or a template literal holding them.
    • Recommend: take only whole, space-separated words from a template literal's static parts. This skips a partial class such as `bg-${color}`.
    • Skip variables, and say so in the README. shadcn's require-static-classes isn't needed for this codebase.

    Its contract

    • No feature scenarios. Tooling testing asks for them only for a deep module that's a program. The readonly and functional plugins have none.
    • Recommend: match those plugins. Write a README, a RuleTester test, a real test through .oxlintrc.json, and a property test where it pays.

    Classifying classes with tailwind-merge

    • tailwind-merge 2.6.1 exports no class-to-group lookup. getDefaultConfig().classGroups holds the groups, but matching a class against them copies tailwind-merge's internal parser.
    • Recommend: probe with twMerge. A class is in a group when a later class of that group, with the same variants, removes it. twMerge('md:-mb-4 md:m-0') drops md:-mb-4, so it's margin.
    • The probe works locally. It placed mx-auto, md:-mb-4, sticky, top-0, w-full, max-w-prose, max-md:hidden and inline-flex correctly. It told flex (display) from flex-1 (item).
    • hidden is in the display group, so check 3 allows it by name.
    • Custom CSS classes match no group. The rule can't see layout set by card, page-column or modal-card, so the README should say so.
    • The probe uses only public API, so it should survive tailwind-merge v3 and Migrate to Tailwind v4 and sort classes with oxfmt #172. Its tests will catch any group renames.
    • No new dependency. tailwind-merge is already one.

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions