Skip to content

Commit e1fe30c

Browse files
authored
Merge pull request #23 from bkrmendy/feat/get-class-order
FEATURE: Expose `getClassOrder` from Tailwind
2 parents fff57cf + 7c8ca5b commit e1fe30c

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

index.d.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,17 @@ export interface Tailwindcss {
5252
* [myHtmlCode]
5353
* )
5454
*/
55-
generateStylesFromContent: (css: string, content: (Content | string)[]) => Promise<string>;
55+
generateStylesFromContent: (css: string, content: (Content | string)[]) => Promise<string>
56+
57+
/**
58+
* Get the class order for the provided list of classes
59+
*
60+
* @param classList The list of classes to get the order for.
61+
* @returns The ordered list of classes.
62+
* @example
63+
* tailwindcss.getClassOrder(['left-3', 'inset-x-2', bg-red-500', 'bg-blue-500'])
64+
*/
65+
getClassOrder: (classList: string[]) => string[]
5666
}
5767

5868
/**
@@ -89,7 +99,7 @@ export interface Content {
8999

90100
/**
91101
* Client side api to generate css via tailwind jit in the browser
92-
*
102+
*
93103
* @deprecated with 0.2.0
94104
*/
95105
declare function jitBrowserTailwindcss(tailwindMainCss: string, jitContent: string, userTailwindConfig?: TailwindConfig): Promise<string>;

src/index.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
import postcss from 'postcss';
22
import processTailwindFeatures from 'tailwindcss/src/processTailwindFeatures.js';
3+
// @ts-ignore
4+
import { createContext } from 'tailwindcss/src/lib/setupContextUtils.js'
35
import resolveConfig from 'tailwindcss/src/public/resolve-config.js';
46

7+
export function bigSign(bigIntValue: bigint) {
8+
return Number(bigIntValue > 0n) - Number(bigIntValue < 0n)
9+
}
10+
11+
function defaultSort(arrayOfTuples: [string, bigint | null][]) {
12+
return arrayOfTuples
13+
.sort(([, a], [, z]) => {
14+
if (a === z) return 0
15+
if (a === null) return -1
16+
if (z === null) return 1
17+
return bigSign(a - z)
18+
})
19+
.map(([className]) => className)
20+
}
21+
522
export const createTailwindcss: typeof import('..').createTailwindcss = (
623
{ tailwindConfig } = {},
724
) => {
@@ -18,9 +35,14 @@ export const createTailwindcss: typeof import('..').createTailwindcss = (
1835
const processor = postcss([tailwindcssPlugin]);
1936
const result = await processor.process(css, { from: undefined });
2037
return result.css;
21-
}
38+
},
39+
40+
getClassOrder: (classList: string[]) => {
41+
const context = createContext(resolveConfig(tailwindConfig ?? {}))
42+
return defaultSort(context.getClassOrder(classList))
43+
},
2244
}
23-
};
45+
}
2446

2547
export const createTailwindcssPlugin: typeof import('..').createTailwindcssPlugin = ({ tailwindConfig, content: contentCollection }) => {
2648
const config = resolveConfig(tailwindConfig ?? {});
@@ -39,3 +61,4 @@ export const jitBrowserTailwindcss: typeof import('..').default = (tailwindMainC
3961
}
4062

4163
export default jitBrowserTailwindcss;
64+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { createTailwindcss } from '../../../dist/module.esm'
2+
3+
test('getClassOrder', async () => {
4+
const tailwind = createTailwindcss({
5+
tailwindConfig: {
6+
corePlugins: { preflight: false },
7+
},
8+
})
9+
10+
const cases = [
11+
{
12+
input: 'px-3 b-class p-1 py-3 bg-blue-500 a-class bg-red-500',
13+
output: 'b-class a-class bg-blue-500 bg-red-500 p-1 px-3 py-3',
14+
},
15+
{
16+
input: 'a-class px-3 p-1 b-class py-3 bg-red-500 bg-blue-500',
17+
output: 'a-class b-class bg-blue-500 bg-red-500 p-1 px-3 py-3',
18+
},
19+
{
20+
input: 'left-5 left-1',
21+
output: 'left-1 left-5',
22+
},
23+
{
24+
input: 'left-3 inset-x-10',
25+
output: 'inset-x-10 left-3',
26+
},
27+
{
28+
input: 'left-3 inset-x-2 bg-red-500 bg-blue-500',
29+
output: 'inset-x-2 left-3 bg-blue-500 bg-red-500',
30+
},
31+
]
32+
33+
for (const { input, output } of cases) {
34+
expect(tailwind.getClassOrder(input.split(' '))).toEqual(output.split(' '))
35+
}
36+
})

types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ declare module 'tailwindcss/src/lib/setupContextUtils.js' {
5555
export interface JitContext {
5656
changedContent: ChangedContent[];
5757
getClassList: () => string[];
58+
getClassOrder: (classList: string[]) => Array<[string, bigint | null]>;
5859
tailwindConfig: TailwindConfig;
5960
variantMap: Map<VariantName, VariantFn[]>;
6061
}
@@ -89,3 +90,4 @@ declare module 'tailwindcss/src/public/resolve-config.js' {
8990

9091
export default function resolveConfig(tailwindConfig: TailwindConfig): TailwindConfig;
9192
}
93+

0 commit comments

Comments
 (0)