@reykjavik/webtools 0.1.34 → 0.1.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,14 @@
4
4
 
5
5
  - ... <!-- Add new lines here. -->
6
6
 
7
+ ## 0.1.35
8
+
9
+ _2024-10-18_
10
+
11
+ - `@reykjavik/webtools/next/vanillaExtract`:
12
+ - feat: `vanillaClass` auto-replaces `.&` tokens in a plain-string input
13
+ - docs: Minor improvements to README and JSDoc comments
14
+
7
15
  ## 0.1.34
8
16
 
9
17
  _2024-10-17_
package/README.md CHANGED
@@ -774,24 +774,42 @@ CSS.
774
774
  Returns a scoped cssClassName styled with free-form CSS. This function is a
775
775
  thin wrapper around vanilla-extract's `style` function.
776
776
 
777
+ When you pass it a string, all `.&` tokens are automatically replaced with the
778
+ selector for the auto-generated class-name. Note that in such cases EVERY
779
+ style property must be wrapped in a selector block.
780
+
781
+ To opt out of the `.&` replacement, use the callback function signature.
782
+
777
783
  ```ts
778
784
  // someFile.css.ts
779
785
  import { vanillaClass } from '@reykjavik/webtools/vanillaExtract';
780
786
 
787
+ // Simple class selector block
781
788
  export const myClass = vanillaClass(`
782
789
  background-color: #ccc;
783
790
  padding: .5em 1em;
784
791
  `);
785
792
 
793
+ // With .& tokens that get replaced with the generated class-name
794
+ export const myClasWithAmp = vanillaClass(`
795
+ .& {
796
+ background-color: #ccc;
797
+ padding: .5em 1em;
798
+ }
799
+ .& > strong {
800
+ color: #c00;
801
+ }
802
+ `);
803
+
786
804
  // Passing a function to get the generated class-name for
787
805
  // more complex styles.
788
806
  export const myOtherClass = vanillaClass(
789
- (className, classNameSelector) => `
807
+ (classNameRaw, classNameSelector) => `
790
808
  ${classNameSelector} {
791
809
  background-color: #ccc;
792
810
  padding: .5em 1em;
793
811
  }
794
- [class="${className}"] > strong {
812
+ [class="${classNameRaw}"] > strong {
795
813
  color: #c00;
796
814
  }
797
815
  @media (min-width: 800px) {
@@ -799,9 +817,12 @@ export const myOtherClass = vanillaClass(
799
817
  background-color: #eee;
800
818
  }
801
819
  }
820
+ /* ".&" tokens in CSS returned from a callback are not replaced */
821
+ .& { will-not-be: interpolated; }
802
822
  `
803
823
  );
804
824
 
825
+ // With a human readable debugId
805
826
  export const humanReadableClass = vanillaClass(
806
827
  'HumanReadable',
807
828
  `
@@ -811,6 +832,9 @@ export const humanReadableClass = vanillaClass(
811
832
  );
812
833
  ```
813
834
 
835
+ (NOTE: The dot-prefixed `.&` pattern is chosen to not conflict with the bare
836
+ `&` token in modern nested CSS.)
837
+
814
838
  ### `vanillaGlobal`
815
839
 
816
840
  **Syntax:** `vanillaGlobal(css: string): void`
@@ -12,9 +12,20 @@ export declare const vanillaGlobal: (css: string) => void;
12
12
  * @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#vanillaprops
13
13
  */
14
14
  export declare const vanillaProps: (css: string) => GlobalStyleRule;
15
- type ClassNameCallback = (className: string, classNameSelector: string) => string;
15
+ type ClassNameCallback = (
16
+ /** The raw standalone class-name. (i.e. `"Component_b6ff51c"`) */
17
+ classNameRaw: string,
18
+ /** The class-name prefixed with a "." for convenence (i.e. `".Component_b6ff51c"`) */
19
+ classNameSelector: string) => string;
16
20
  /**
17
- * Returns a scoped cssClassName styled with free-form CSS
21
+ * Returns a scoped cssClassName styled with free-form CSS. This function is a
22
+ * thin wrapper around vanilla-extract's `style` function.
23
+ *
24
+ * When you pass it a string, all `.&` tokens are automatically replaced with the
25
+ * selector for the auto-generated class-name. Note that in such cases EVERY
26
+ * style property must be wrapped in a selector block.
27
+ *
28
+ * To opt out of the `.&` replacement, use the callback function signature.
18
29
  *
19
30
  * @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#vanillaclass
20
31
  */
@@ -16,12 +16,14 @@ export const vanillaProps = (css) => ({ x: `; ${css}` });
16
16
  export function vanillaClass(cssOrDebugId, css) {
17
17
  const debugId = css != null ? cssOrDebugId : undefined;
18
18
  css = css != null ? css : cssOrDebugId;
19
- if (typeof css === 'function') {
20
- const className = style({}, debugId);
21
- vanillaGlobal(css(className, `.${className}`));
22
- return className;
19
+ if (typeof css === 'string' && !/.&/.test(css)) {
20
+ return style(vanillaProps(css), debugId);
23
21
  }
24
- return style(vanillaProps(css), debugId);
22
+ const className = style({}, debugId);
23
+ vanillaGlobal(typeof css === 'function'
24
+ ? css('className', `.${className}`)
25
+ : css.replace(/.&/g, `.${className}`));
26
+ return className;
25
27
  }
26
28
  // ---------------------------------------------------------------------------
27
29
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reykjavik/webtools",
3
- "version": "0.1.34",
3
+ "version": "0.1.35",
4
4
  "description": "Misc. JS/TS helpers used by Reykjavík City's web dev teams.",
5
5
  "main": "index.js",
6
6
  "repository": "ssh://git@github.com:reykjavikcity/webtools.git",
@@ -12,9 +12,20 @@ export declare const vanillaGlobal: (css: string) => void;
12
12
  * @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#vanillaprops
13
13
  */
14
14
  export declare const vanillaProps: (css: string) => GlobalStyleRule;
15
- type ClassNameCallback = (className: string, classNameSelector: string) => string;
15
+ type ClassNameCallback = (
16
+ /** The raw standalone class-name. (i.e. `"Component_b6ff51c"`) */
17
+ classNameRaw: string,
18
+ /** The class-name prefixed with a "." for convenence (i.e. `".Component_b6ff51c"`) */
19
+ classNameSelector: string) => string;
16
20
  /**
17
- * Returns a scoped cssClassName styled with free-form CSS
21
+ * Returns a scoped cssClassName styled with free-form CSS. This function is a
22
+ * thin wrapper around vanilla-extract's `style` function.
23
+ *
24
+ * When you pass it a string, all `.&` tokens are automatically replaced with the
25
+ * selector for the auto-generated class-name. Note that in such cases EVERY
26
+ * style property must be wrapped in a selector block.
27
+ *
28
+ * To opt out of the `.&` replacement, use the callback function signature.
18
29
  *
19
30
  * @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#vanillaclass
20
31
  */
package/vanillaExtract.js CHANGED
@@ -21,12 +21,14 @@ exports.vanillaProps = vanillaProps;
21
21
  function vanillaClass(cssOrDebugId, css) {
22
22
  const debugId = css != null ? cssOrDebugId : undefined;
23
23
  css = css != null ? css : cssOrDebugId;
24
- if (typeof css === 'function') {
25
- const className = (0, css_1.style)({}, debugId);
26
- (0, exports.vanillaGlobal)(css(className, `.${className}`));
27
- return className;
24
+ if (typeof css === 'string' && !/.&/.test(css)) {
25
+ return (0, css_1.style)((0, exports.vanillaProps)(css), debugId);
28
26
  }
29
- return (0, css_1.style)((0, exports.vanillaProps)(css), debugId);
27
+ const className = (0, css_1.style)({}, debugId);
28
+ (0, exports.vanillaGlobal)(typeof css === 'function'
29
+ ? css('className', `.${className}`)
30
+ : css.replace(/.&/g, `.${className}`));
31
+ return className;
30
32
  }
31
33
  exports.vanillaClass = vanillaClass;
32
34
  // ---------------------------------------------------------------------------