Skip to content

Commit 0adf201

Browse files
committed
feat: add auto margin support (m-auto, mx-auto, my-auto, etc.)
1 parent 55363ba commit 0adf201

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/parser/spacing.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,43 @@ describe("parseSpacing - logical padding (RTL-aware)", () => {
416416
});
417417
});
418418

419+
describe("parseSpacing - auto margin", () => {
420+
it("should parse m-auto", () => {
421+
expect(parseSpacing("m-auto")).toEqual({ margin: "auto" });
422+
});
423+
424+
it("should parse mx-auto", () => {
425+
expect(parseSpacing("mx-auto")).toEqual({ marginHorizontal: "auto" });
426+
});
427+
428+
it("should parse my-auto", () => {
429+
expect(parseSpacing("my-auto")).toEqual({ marginVertical: "auto" });
430+
});
431+
432+
it("should parse directional auto margins", () => {
433+
expect(parseSpacing("mt-auto")).toEqual({ marginTop: "auto" });
434+
expect(parseSpacing("mr-auto")).toEqual({ marginRight: "auto" });
435+
expect(parseSpacing("mb-auto")).toEqual({ marginBottom: "auto" });
436+
expect(parseSpacing("ml-auto")).toEqual({ marginLeft: "auto" });
437+
});
438+
439+
it("should parse logical auto margins (RTL-aware)", () => {
440+
expect(parseSpacing("ms-auto")).toEqual({ marginStart: "auto" });
441+
expect(parseSpacing("me-auto")).toEqual({ marginEnd: "auto" });
442+
});
443+
444+
it("should not parse padding auto (not valid in Tailwind)", () => {
445+
expect(parseSpacing("p-auto")).toBeNull();
446+
expect(parseSpacing("px-auto")).toBeNull();
447+
expect(parseSpacing("py-auto")).toBeNull();
448+
expect(parseSpacing("pt-auto")).toBeNull();
449+
});
450+
451+
it("should not parse gap auto (not valid in Tailwind)", () => {
452+
expect(parseSpacing("gap-auto")).toBeNull();
453+
});
454+
});
455+
419456
describe("parseSpacing - custom spacing", () => {
420457
const customSpacing = {
421458
xs: 4,

src/parser/spacing.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,22 @@ function parseArbitrarySpacing(value: string): number | null {
6969

7070
/**
7171
* Parse spacing classes (margin, padding, gap)
72-
* Examples: m-4, mx-2, mt-8, p-4, px-2, pt-8, gap-4, m-[16px], pl-[4.5px], -m-4, -mt-[10px], ms-4, pe-2
72+
* Examples: m-4, mx-2, mt-8, p-4, px-2, pt-8, gap-4, m-[16px], pl-[4.5px], -m-4, -mt-[10px], ms-4, pe-2, mx-auto
7373
* @param cls - The class name to parse
7474
* @param customSpacing - Optional custom spacing values from tailwind.config
7575
*/
7676
export function parseSpacing(cls: string, customSpacing?: Record<string, number>): StyleObject | null {
7777
// Merge custom spacing with defaults (custom takes precedence)
7878
const spacingMap = customSpacing ? { ...SPACING_SCALE, ...customSpacing } : SPACING_SCALE;
7979

80+
// Auto margins: m-auto, mx-auto, my-auto, mt-auto, mr-auto, mb-auto, ml-auto, ms-auto, me-auto
81+
// Note: Only margins support auto values (not padding or gap)
82+
const autoMarginMatch = cls.match(/^m([xytrblse]?)-auto$/);
83+
if (autoMarginMatch) {
84+
const dir = autoMarginMatch[1];
85+
return getMarginStyle(dir, "auto");
86+
}
87+
8088
// Margin: m-4, mx-2, mt-8, ms-4, me-2, m-[16px], -m-4, -mt-2, etc.
8189
// Supports negative values for margins (but not padding or gap)
8290
// s = start (RTL-aware), e = end (RTL-aware)
@@ -143,7 +151,7 @@ export function parseSpacing(cls: string, customSpacing?: Record<string, number>
143151
/**
144152
* Get margin style object based on direction
145153
*/
146-
function getMarginStyle(dir: string, value: number): StyleObject {
154+
function getMarginStyle(dir: string, value: number | "auto"): StyleObject {
147155
switch (dir) {
148156
case "":
149157
return { margin: value };

0 commit comments

Comments
 (0)