Skip to content

Commit

Permalink
feat: Better support for data attributes (#106)
Browse files Browse the repository at this point in the history
feat: Add `if` overloads for attribute selectors.
  • Loading branch information
stephenh committed May 8, 2023
1 parent a6e5e35 commit bf7c0b2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
9 changes: 6 additions & 3 deletions packages/template-tachyons/src/Css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2451,11 +2451,14 @@ class CssBuilder<T extends Properties> {

if(bp: Breakpoint): CssBuilder<T>;
if(cond: boolean): CssBuilder<T>;
if(arg: boolean | Breakpoint): CssBuilder<T> {
if (typeof arg === "boolean") {
if(attr: string, value: boolean | string): CssBuilder<T>;
if(arg: boolean | Breakpoint | string, value?: boolean | string): CssBuilder<T> {
if (value !== undefined) {
return this.newCss({ selector: `[${arg}='${value}']` });
} else if (typeof arg === "boolean") {
return this.newCss({ enabled: arg });
} else {
return this.newCss({ selector: Breakpoints[arg] });
return this.newCss({ selector: Breakpoints[arg as Breakpoint] });
}
}

Expand Down
18 changes: 18 additions & 0 deletions packages/testing-tachyons/src/Css.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,24 @@ describe("Css", () => {
`);
});

it("can use data attributes", () => {
// Maybe Css.black.ifDataActive.blue.$ ?
// Maybe Css.black.ifData("active", "true").blue.$
// Maybe Css.black.if("data-active", "true").blue.$ <-- trying this one
// Maybe Css.black.if("data-active='true'").blue.$
expect(Css.black.if("data-active", "true").blue.if("data-active", false).primary.$).toMatchInlineSnapshot(`
{
"[data-active='false']": {
"color": "var(--primary)",
},
"[data-active='true']": {
"color": "#526675",
},
"color": "#353535",
}
`);
});

it("can use sqPx", () => {
expect(Css.sqPx(24).$).toMatchInlineSnapshot(`
{
Expand Down
9 changes: 6 additions & 3 deletions packages/testing-tachyons/src/Css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2150,11 +2150,14 @@ class CssBuilder<T extends Properties> {

if(bp: Breakpoint): CssBuilder<T>;
if(cond: boolean): CssBuilder<T>;
if(arg: boolean | Breakpoint): CssBuilder<T> {
if (typeof arg === "boolean") {
if(attr: string, value: boolean | string): CssBuilder<T>;
if(arg: boolean | Breakpoint | string, value?: boolean | string): CssBuilder<T> {
if (value !== undefined) {
return this.newCss({ selector: `[${arg}='${value}']` });
} else if (typeof arg === "boolean") {
return this.newCss({ enabled: arg });
} else {
return this.newCss({ selector: Breakpoints[arg] });
return this.newCss({ selector: Breakpoints[arg as Breakpoint] });
}
}

Expand Down
9 changes: 6 additions & 3 deletions packages/truss/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ class CssBuilder<T extends Properties> {
if(bp: Breakpoint): CssBuilder<T>;
if(cond: boolean): CssBuilder<T>;
if(arg: boolean | Breakpoint): CssBuilder<T> {
if (typeof arg === "boolean") {
if(attr: string, value: boolean | string): CssBuilder<T>;
if(arg: boolean | Breakpoint | string, value?: boolean | string): CssBuilder<T> {
if (value !== undefined) {
return this.newCss({ selector: \`[\${arg}='\${value}']\` });
} else if (typeof arg === "boolean") {
return this.newCss({ enabled: arg });
} else {
return this.newCss({ selector: Breakpoints[arg] });
return this.newCss({ selector: Breakpoints[arg as Breakpoint] });
}
}
Expand Down

0 comments on commit bf7c0b2

Please sign in to comment.