@prismora/elements 0.1.1 → 0.1.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.
Files changed (2) hide show
  1. package/dist/catalog.js +43 -6
  2. package/package.json +3 -3
package/dist/catalog.js CHANGED
@@ -23,8 +23,15 @@ function reactStyle(css) {
23
23
  const key = prop.startsWith('--') ? `'${prop}'` : prop.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
24
24
  return `${key}: '${val}'`;
25
25
  });
26
+ // React's style type has no custom properties: cast when the style sets --pm-* variables.
27
+ if (parts.some((p) => p.startsWith("'--"))) {
28
+ usesCssProperties = true;
29
+ return `{{ ${parts.join(', ')} } as CSSProperties}`;
30
+ }
26
31
  return `{{ ${parts.join(', ')} }}`;
27
32
  }
33
+ /** Set by reactStyle while rendering one snippet (adds `import type { CSSProperties } from 'react'`). */
34
+ let usesCssProperties = false;
28
35
  export const FRAMEWORKS = ['html', 'react', 'vue', 'svelte', 'solid', 'angular'];
29
36
  /** Self-contained sample picture for image demos (no network request). */
30
37
  const IMG = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 90'%3E%3Cdefs%3E%3ClinearGradient id='g' x2='1' y2='1'%3E%3Cstop stop-color='%237c5cff'/%3E%3Cstop offset='1' stop-color='%23ff7ab6'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect width='160' height='90' fill='url(%23g)'/%3E%3Ccircle cx='118' cy='30' r='14' fill='%23fff' opacity='.85'/%3E%3Cpath d='M0 90l45-48 30 30 22-18 63 36z' fill='%23fff' opacity='.35'/%3E%3C/svg%3E";
@@ -698,6 +705,11 @@ function render(nodes, fw, depth = 0) {
698
705
  const wrap = n.tag.startsWith('pm-') && fw !== 'html' && fw !== 'solid' && fw !== 'angular';
699
706
  const name = wrap ? pascalName(n.tag) : n.tag;
700
707
  const attrs = n.attrs.map(([k, v]) => {
708
+ // Array attributes written as comma lists (labels="Jan,Feb") are typed arrays on the wrappers.
709
+ if (typeof v === 'string' && wrap && !isJson(v) && attrType(n.tag, k) === 'json' && v.includes(',')) {
710
+ const items = v.split(',').map((x) => x.trim());
711
+ v = JSON.stringify(items.every((x) => x !== '' && !Number.isNaN(Number(x))) ? items.map(Number) : items);
712
+ }
701
713
  // JSON-valued attributes (e.g. data-table columns/rows) become real values in frameworks.
702
714
  if (typeof v === 'string' && isJson(v) && n.tag.startsWith('pm-')) {
703
715
  if (fw === 'react' || (fw === 'svelte' && wrap))
@@ -706,6 +718,9 @@ function render(nodes, fw, depth = 0) {
706
718
  return `:${k}='${v}'`;
707
719
  return `${k}='${v}'`;
708
720
  }
721
+ // popover="" means auto; JSX typings (Preact's in particular) only accept the keyword.
722
+ if ((fw === 'react' || fw === 'solid') && k === 'popover' && (v === true || v === ''))
723
+ v = 'auto';
709
724
  if (fw === 'react') {
710
725
  const key = k === 'class' ? 'className' : k === 'for' && !wrap ? 'htmlFor' : wrap ? propName(k) : k;
711
726
  if (v === true)
@@ -760,6 +775,21 @@ function render(nodes, fw, depth = 0) {
760
775
  return `${pad}${open}>\n${render(n.children, fw, depth + 1)}\n${pad}</${name}>`;
761
776
  }).join('\n');
762
777
  }
778
+ /** Icon names the markup uses (pm-icon name, icon / icon-start / icon-end on any element): they must be registered. */
779
+ function usedIcons(nodes, acc = new Set()) {
780
+ for (const n of nodes) {
781
+ if (typeof n === 'string')
782
+ continue;
783
+ for (const [k, v] of n.attrs) {
784
+ if (typeof v !== 'string' || !/^[a-z0-9-]+$/.test(v))
785
+ continue;
786
+ if ((n.tag === 'pm-icon' && k === 'name') || (n.tag.startsWith('pm-') && ['icon', 'icon-start', 'icon-end'].includes(k)))
787
+ acc.add(v);
788
+ }
789
+ usedIcons(n.children, acc);
790
+ }
791
+ return acc;
792
+ }
763
793
  function usedTags(nodes, acc = new Set()) {
764
794
  for (const n of nodes) {
765
795
  if (typeof n === 'string')
@@ -777,27 +807,34 @@ function usedTags(nodes, acc = new Set()) {
777
807
  */
778
808
  export function snippet(html, fw, name = 'Demo') {
779
809
  const nodes = parse(html);
810
+ usesCssProperties = false;
780
811
  const body = render(nodes, fw);
812
+ const iconNames = [...usedIcons(nodes)].sort().map(camel);
813
+ // Named icons render only once registered; register exactly the ones used (the rest stay out of the bundle).
814
+ const iconImports = iconNames.length ? [`import { registerIcons } from '@prismora/elements/icon';`, `import { ${iconNames.join(', ')} } from '@prismora/icons';`] : [];
815
+ const iconCall = iconNames.length ? `registerIcons([${iconNames.join(', ')}]);` : '';
816
+ const iconBlock = (indent = '') => (iconNames.length ? `${[...iconImports, '', iconCall].map((l) => (l ? indent + l : l)).join('\n')}\n` : '');
781
817
  const tags = [...usedTags(nodes)];
782
818
  const names = [...new Set(tags.map(pascalName))].sort().join(', ');
783
819
  // Import only the elements used, so bundles contain nothing else.
784
820
  const imports = [...new Set(tags.map((t) => moduleFor(t) ?? '@prismora/elements/define'))].sort().map((m) => `import '${m}';`);
785
821
  switch (fw) {
786
822
  case 'html':
787
- return `<link rel="stylesheet" href="@prismora/css">\n<script type="module">\n ${imports.join('\n ')}\n</script>\n\n${body}`;
823
+ // Imported through the module script so any bundler (Vite, Parcel, …) resolves the package; a <link href> cannot.
824
+ return `<script type="module">\n import '@prismora/css';\n ${imports.join('\n ')}\n${iconBlock(' ')}</script>\n\n${body}`;
788
825
  case 'react':
789
- return `import { ${names} } from '@prismora/react';\n\nexport default function ${name}() {\n return (\n <>\n${body.split('\n').map((l) => ` ${l}`).join('\n')}\n </>\n );\n}`;
826
+ return `${usesCssProperties ? "import type { CSSProperties } from 'react';\n" : ''}import { ${names} } from '@prismora/react';\n${iconBlock()}\nexport default function ${name}() {\n return (\n <>\n${body.split('\n').map((l) => ` ${l}`).join('\n')}\n </>\n );\n}`;
790
827
  case 'vue':
791
- return `<script setup lang="ts">\nimport { ${names} } from '@prismora/vue';\n</script>\n\n<template>\n${body.split('\n').map((l) => ` ${l}`).join('\n')}\n</template>`;
828
+ return `<script setup lang="ts">\nimport { ${names} } from '@prismora/vue';\n${iconBlock()}</script>\n\n<template>\n${body.split('\n').map((l) => ` ${l}`).join('\n')}\n</template>`;
792
829
  case 'svelte':
793
- return `<script lang="ts">\n import { ${names} } from '@prismora/svelte';\n</script>\n\n${body}`;
830
+ return `<script lang="ts">\n import { ${names} } from '@prismora/svelte';\n${iconBlock(' ')}</script>\n\n${body}`;
794
831
  case 'angular': {
795
832
  // Raw <pm-*> tags in a standalone component; CUSTOM_ELEMENTS_SCHEMA lets the compiler accept them.
796
833
  const selector = name.replace(/Page$/, '').replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
797
834
  const template = body.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$\{/g, '\\${').split('\n').map((l) => ` ${l}`).join('\n');
798
- return `import { CUSTOM_ELEMENTS_SCHEMA, Component } from '@angular/core';\n${imports.join('\n')}\n\n@Component({\n selector: 'app-${selector}',\n schemas: [CUSTOM_ELEMENTS_SCHEMA],\n template: \`\n${template}\n \`,\n})\nexport class ${name}Component {}`;
835
+ return `import { CUSTOM_ELEMENTS_SCHEMA, Component } from '@angular/core';\n${imports.join('\n')}\n${iconBlock()}\n@Component({\n selector: 'app-${selector}',\n schemas: [CUSTOM_ELEMENTS_SCHEMA],\n template: \`\n${template}\n \`,\n})\nexport class ${name}Component {}`;
799
836
  }
800
837
  case 'solid':
801
- return `import type {} from '@prismora/solid'; // JSX typings\n${imports.join('\n')}\n\nexport default function ${name}() {\n return (\n <>\n${body.split('\n').map((l) => ` ${l}`).join('\n')}\n </>\n );\n}`;
838
+ return `import type {} from '@prismora/solid'; // JSX typings\n${imports.join('\n')}\n${iconBlock()}\nexport default function ${name}() {\n return (\n <>\n${body.split('\n').map((l) => ` ${l}`).join('\n')}\n </>\n );\n}`;
802
839
  }
803
840
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismora/elements",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Prismora web components (Lit). Work in any framework or plain HTML.",
5
5
  "keywords": [
6
6
  "prismora",
@@ -91,8 +91,8 @@
91
91
  "./dist/*.js"
92
92
  ],
93
93
  "dependencies": {
94
- "@prismora/css": "^0.1.1",
95
- "@prismora/motion": "^0.1.1",
94
+ "@prismora/css": "^0.1.3",
95
+ "@prismora/motion": "^0.1.3",
96
96
  "lit": "^3",
97
97
  "@lit/reactive-element": "^2.1.0"
98
98
  },