Skip to content

Commit

Permalink
Allow variant registration with custom order
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-spiess committed Sep 13, 2024
1 parent e9c913a commit c8889d5
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions packages/tailwindcss/src/variants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ export class Variants {
*/
private lastOrder = 0

static(name: string, applyFn: VariantFn<'static'>, { compounds }: { compounds?: boolean } = {}) {
this.set(name, { kind: 'static', applyFn, compounds: compounds ?? true })
static(
name: string,
applyFn: VariantFn<'static'>,
{ compounds, order }: { compounds?: boolean; order?: number } = {},
) {
this.set(name, { kind: 'static', applyFn, compounds: compounds ?? true, order })
}

fromAst(name: string, ast: AstNode[]) {
Expand All @@ -53,17 +57,17 @@ export class Variants {
functional(
name: string,
applyFn: VariantFn<'functional'>,
{ compounds }: { compounds?: boolean } = {},
{ compounds, order }: { compounds?: boolean; order?: number } = {},
) {
this.set(name, { kind: 'functional', applyFn, compounds: compounds ?? true })
this.set(name, { kind: 'functional', applyFn, compounds: compounds ?? true, order })
}

compound(
name: string,
applyFn: VariantFn<'compound'>,
{ compounds }: { compounds?: boolean } = {},
{ compounds, order }: { compounds?: boolean; order?: number } = {},
) {
this.set(name, { kind: 'compound', applyFn, compounds: compounds ?? true })
this.set(name, { kind: 'compound', applyFn, compounds: compounds ?? true, order })
}

group(fn: () => void, compareFn?: CompareFn) {
Expand Down Expand Up @@ -136,17 +140,25 @@ export class Variants {

private set<T extends Variant['kind']>(
name: string,
{ kind, applyFn, compounds }: { kind: T; applyFn: VariantFn<T>; compounds: boolean },
{
kind,
applyFn,
compounds,
order,
}: { kind: T; applyFn: VariantFn<T>; compounds: boolean; order?: number },
) {
let existing = this.variants.get(name)
if (existing) {
Object.assign(existing, { kind, applyFn, compounds })
} else {
this.lastOrder = this.nextOrder()
if (order === undefined) {
this.lastOrder = this.nextOrder()
order = this.lastOrder
}
this.variants.set(name, {
kind,
applyFn,
order: this.lastOrder,
order,
compounds,
})
}
Expand Down

0 comments on commit c8889d5

Please sign in to comment.