@reykjavik/webtools 0.1.33 → 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,22 @@
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
+
15
+ ## 0.1.34
16
+
17
+ _2024-10-17_
18
+
19
+ - `@reykjavik/webtools/next/vanillaExtract`:
20
+ - feat: `vanillaClass` passes `classNameSelector` a second parameter to the
21
+ render callback.
22
+
7
23
  ## 0.1.33
8
24
 
9
25
  _2024-10-16_
package/README.md CHANGED
@@ -767,41 +767,62 @@ CSS.
767
767
  ### `vanillaClass`
768
768
 
769
769
  **Syntax:**
770
- `vanillaClass(css: string | ((className: string) => string)): string`
770
+ `vanillaClass(css: string | ((className: string, classNameSelector: string) => string)): string`
771
771
  **Syntax:**
772
- `vanillaClass(debugId: string, css: string | ((className: string) => string)): string`
772
+ `vanillaClass(debugId: string, css: string | ((className: string, classNameSelector: string) => string)): string`
773
773
 
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
 
786
- // Passing a function to get the generated class name for
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
+
804
+ // Passing a function to get the generated class-name for
787
805
  // more complex styles.
788
806
  export const myOtherClass = vanillaClass(
789
- (className) => `
790
- .${className} {
807
+ (classNameRaw, classNameSelector) => `
808
+ ${classNameSelector} {
791
809
  background-color: #ccc;
792
810
  padding: .5em 1em;
793
811
  }
794
- .${className} > strong {
812
+ [class="${classNameRaw}"] > strong {
795
813
  color: #c00;
796
814
  }
797
815
  @media (min-width: 800px) {
798
- .${className} {
816
+ ${classNameSelector} {
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`
@@ -1,8 +1,8 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  import React, { FunctionComponent } from 'react';
3
3
  import type { Cleanup } from '@reykjavik/hanna-utils';
4
- import type { ServerResponse } from 'http';
5
4
  import type { AppType } from 'next/app.js';
5
+ import type { ServerResponse } from 'node:http';
6
6
  import type { HTTP_ERROR_ALL, TTLConfig } from '../http.js';
7
7
  export * from '../http.js';
8
8
  type NextContextLike = {
@@ -12,13 +12,25 @@ 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 = (
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;
15
20
  /**
16
- * 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.
17
29
  *
18
30
  * @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#vanillaclass
19
31
  */
20
- export declare function vanillaClass(css: string | ((className: string) => string)): string;
21
- export declare function vanillaClass(debugId: string, css: string | ((className: string) => string)): string;
32
+ export declare function vanillaClass(css: string | ClassNameCallback): string;
33
+ export declare function vanillaClass(debugId: string, css: string | ClassNameCallback): string;
22
34
  /**
23
35
  * @deprecated (Will be removed in v0.2)
24
36
  *
@@ -148,3 +160,4 @@ export declare function vanillaClassNested(css: string): string;
148
160
  * `postcss` config.
149
161
  */
150
162
  export declare function vanillaClassNested(debugId: string, css: string): string;
163
+ export {};
@@ -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));
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/next/http.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  /// <reference types="node" />
2
2
  import React, { FunctionComponent } from 'react';
3
3
  import type { Cleanup } from '@reykjavik/hanna-utils';
4
- import type { ServerResponse } from 'http';
5
4
  import type { AppType } from 'next/app.js';
5
+ import type { ServerResponse } from 'node:http';
6
6
  import type { HTTP_ERROR_ALL, TTLConfig } from '../http.js';
7
7
  export * from '../http.js';
8
8
  type NextContextLike = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reykjavik/webtools",
3
- "version": "0.1.33",
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,13 +12,25 @@ 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 = (
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;
15
20
  /**
16
- * 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.
17
29
  *
18
30
  * @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#vanillaclass
19
31
  */
20
- export declare function vanillaClass(css: string | ((className: string) => string)): string;
21
- export declare function vanillaClass(debugId: string, css: string | ((className: string) => string)): string;
32
+ export declare function vanillaClass(css: string | ClassNameCallback): string;
33
+ export declare function vanillaClass(debugId: string, css: string | ClassNameCallback): string;
22
34
  /**
23
35
  * @deprecated (Will be removed in v0.2)
24
36
  *
@@ -148,3 +160,4 @@ export declare function vanillaClassNested(css: string): string;
148
160
  * `postcss` config.
149
161
  */
150
162
  export declare function vanillaClassNested(debugId: string, css: string): string;
163
+ export {};
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));
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
  // ---------------------------------------------------------------------------