poseui 0.0.1 → 0.0.3

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/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { GenerateOptions } from "@unocss/core";
2
+
1
3
  //#region src/index.d.ts
2
4
  interface StandardSchemaV1<Input = unknown, Output = Input> {
3
5
  readonly "~standard": StandardSchemaV1.Props<Input, Output>;
@@ -39,13 +41,13 @@ declare class PoseValidationError extends Error {
39
41
  constructor(issues: ReadonlyArray<StandardSchemaV1.Issue>);
40
42
  }
41
43
  type Dyn<TProps, T> = T | ((props: TProps) => T);
42
- type ChildValue = string | number | PoseElement<any, any> | Array<string | number | PoseElement<any, any>>;
44
+ type ChildValue = string | number | undefined | null | PoseElement<any, any> | Array<string | number | PoseElement<any, any> | undefined | null>;
43
45
  type Child<TProps> = ChildValue | ((props: TProps) => ChildValue);
44
46
  type RenderReturn<TSchema extends StandardSchemaV1 | undefined> = TSchema extends StandardSchemaV1 ? ReturnType<TSchema["~standard"]["validate"]> extends Promise<any> ? Promise<string> : string : string;
45
- type CallArgs<TProps extends Record<string, unknown>> = [keyof TProps] extends [never] ? [] | [TProps] : [TProps];
47
+ type CallArgs<TProps extends Record<string, unknown>, TSchema> = TSchema extends StandardSchemaV1 ? [TProps?] : [keyof TProps] extends [never] ? [TProps?] : [TProps];
46
48
  type ClassEntry<TProps> = string | ((props: TProps) => string);
47
49
  interface PoseElement<TProps extends Record<string, unknown>, TSchema extends StandardSchemaV1 | undefined = undefined> {
48
- (...args: CallArgs<TProps>): RenderReturn<TSchema>;
50
+ (...args: CallArgs<TProps, TSchema>): RenderReturn<TSchema>;
49
51
  readonly classes: ReadonlyArray<ClassEntry<TProps>>;
50
52
  /**
51
53
  * Bind a Standard Schema (Zod, Valibot, ArkType, …).
@@ -780,17 +782,17 @@ interface PoseElement<TProps extends Record<string, unknown>, TSchema extends St
780
782
  * Apply styles conditionally — predicate form or value-switch form.
781
783
  *
782
784
  * Predicate form (apply when true):
783
- * ```ts
785
+ * \`\`\`ts
784
786
  * .when(({ disabled }) => disabled, (b) => b.opacity(50).cursor_not_allowed())
785
- * ```
787
+ * \`\`\`
786
788
  *
787
789
  * Value form (switch on a prop key):
788
- * ```ts
790
+ * \`\`\`ts
789
791
  * .when("variant", {
790
792
  * primary: (b) => b.bg("indigo-600").text_color("white"),
791
793
  * secondary: (b) => b.bg("slate-200").text_color("slate-900"),
792
794
  * })
793
- * ```
795
+ * \`\`\`
794
796
  * Cases are Partial — unmatched values emit no classes.
795
797
  * Multiple .when() calls are independent and all evaluated at render time.
796
798
  */
@@ -806,11 +808,13 @@ interface PoseElement<TProps extends Record<string, unknown>, TSchema extends St
806
808
  child(fn: (props: TProps) => ChildValue): PoseElement<TProps, TSchema>;
807
809
  child(value: ChildValue): PoseElement<TProps, TSchema>;
808
810
  /**
809
- * Render to `{ html, css }` using UnoCSS + presetWind4.
811
+ * Render to \`{ html, css }\` using UnoCSS + presetWind4.
810
812
  * @example
811
813
  * const { html, css } = await card.render({ name: 'Ada' })
812
814
  */
813
- render(...args: CallArgs<TProps>): Promise<{
815
+ render(props?: CallArgs<TProps, TSchema>[0], opts?: {
816
+ generatorOptions?: Partial<GenerateOptions<false>>;
817
+ }): Promise<{
814
818
  html: string;
815
819
  css: string;
816
820
  }>;
package/dist/index.js CHANGED
@@ -31,11 +31,11 @@ function resolveClasses(classes, props) {
31
31
  }
32
32
  function renderChild(child, props) {
33
33
  if (typeof child === "function") return renderChild(child(props), props);
34
- if (Array.isArray(child)) return child.map((c) => renderChild(c, props)).join("");
34
+ if (Array.isArray(child)) return child.filter((c) => c != null).map((c) => renderChild(c, props)).join("");
35
35
  if (child != null && typeof child === "object" && "__pose" in child) return child(props);
36
36
  return child == null ? "" : String(child);
37
37
  }
38
- /** A tagless builder used inside .when() callbacks — only accumulates classes. */
38
+ /** A tagless builder used inside .when() callbacks — only accumulates classes and children. */
39
39
  function createBlankBuilder() {
40
40
  return createBuilder({
41
41
  tag: "div",
@@ -76,6 +76,7 @@ function createBuilder(state) {
76
76
  return buildHtml(result);
77
77
  }
78
78
  render.__pose = true;
79
+ render.__state = state;
79
80
  const el = render;
80
81
  Object.defineProperty(el, "classes", {
81
82
  get: () => state.classes,
@@ -464,27 +465,37 @@ function createBuilder(state) {
464
465
  el.stroke_w = (n) => dynCls(n, (v) => tw("stroke", v));
465
466
  el.sr_only = () => cls("sr-only");
466
467
  el.not_sr_only = () => cls("not-sr-only");
468
+ function applyBranch(getBranch) {
469
+ const classEntry = (props) => {
470
+ const branch = getBranch(props);
471
+ return branch ? resolveClasses(branch.classes, props) : "";
472
+ };
473
+ const childEntry = (props) => {
474
+ const branch = getBranch(props);
475
+ if (!branch) return null;
476
+ const branchState = branch.__state;
477
+ if (!branchState.children.length) return null;
478
+ return branchState.children.map((c) => typeof c === "function" ? c(props) : c);
479
+ };
480
+ return derive([classEntry], [childEntry]);
481
+ }
467
482
  el.when = (...args) => {
468
483
  if (typeof args[0] === "function") {
469
484
  const [pred, apply] = args;
470
- return derive([(props) => {
471
- if (!pred(props)) return "";
472
- return resolveClasses(apply(createBlankBuilder()).classes, props);
473
- }]);
485
+ return applyBranch((props) => pred(props) ? apply(createBlankBuilder()) : null);
474
486
  } else {
475
487
  const [key, cases] = args;
476
- return derive([(props) => {
488
+ return applyBranch((props) => {
477
489
  const branch = cases[props[key]];
478
- if (!branch) return "";
479
- return resolveClasses(branch(createBlankBuilder()).classes, props);
480
- }]);
490
+ return branch ? branch(createBlankBuilder()) : null;
491
+ });
481
492
  }
482
493
  };
483
494
  el.cls = (value) => typeof value === "function" ? derive([value]) : derive([value]);
484
- el.render = async (...args) => {
485
- const html = render(...args);
495
+ el.render = async (props, opts) => {
496
+ const html = render(props);
486
497
  const resolvedHtml = html instanceof Promise ? await html : html;
487
- const { css } = await (await getGenerator()).generate(resolvedHtml, { preflights: false });
498
+ const { css } = await (await getGenerator()).generate(resolvedHtml, opts?.generatorOptions);
488
499
  return {
489
500
  html: resolvedHtml,
490
501
  css
@@ -497,7 +508,7 @@ function createBuilder(state) {
497
508
  });
498
509
  return el;
499
510
  }
500
- let _generator = null;
511
+ let _generator = void 0;
501
512
  async function getGenerator() {
502
513
  if (_generator) return _generator;
503
514
  const { createGenerator } = await import("@unocss/core");
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "poseui",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Type-safe HTML templating engine on steroids",
5
5
  "license": "MIT",
6
6
  "author": "Ryuz <ryuzer@proton.me>",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/guarana-studio/poseui.git"
9
+ "url": "git+https://github.com/guarana-studio/pose.git"
10
10
  },
11
11
  "files": [
12
12
  "dist"