@mgdis/core-ui-helpers 1.1.2 → 1.2.1

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 (37) hide show
  1. package/.turbo/turbo-build.log +17 -20
  2. package/CHANGELOG.md +17 -0
  3. package/dist/playwright/index.cjs +11 -5
  4. package/dist/playwright/index.cjs.map +1 -1
  5. package/dist/playwright/index.d.ts +7 -3
  6. package/dist/playwright/index.js +11 -5
  7. package/dist/playwright/index.js.map +1 -1
  8. package/dist/playwright/index.umd.js +11 -5
  9. package/dist/playwright/index.umd.js.map +1 -1
  10. package/dist/stencil/index.cjs +153 -13
  11. package/dist/stencil/index.cjs.map +1 -1
  12. package/dist/stencil/index.d.ts +113 -2
  13. package/dist/stencil/index.js +152 -14
  14. package/dist/stencil/index.js.map +1 -1
  15. package/dist/stencil/index.umd.js +153 -13
  16. package/dist/stencil/index.umd.js.map +1 -1
  17. package/dist/storybook/index.cjs +2 -2
  18. package/dist/storybook/index.cjs.map +1 -1
  19. package/dist/storybook/index.js +2 -2
  20. package/dist/storybook/index.js.map +1 -1
  21. package/dist/storybook/index.umd.js +2 -2
  22. package/dist/storybook/index.umd.js.map +1 -1
  23. package/json-doc.test.json +13 -2
  24. package/package.json +13 -13
  25. package/src/playwright/utils/__snapshots__/utils.spec.ts.snap +3 -3
  26. package/src/playwright/utils/utils.ts +10 -5
  27. package/src/stencil/ide/__snapshots__/cem.spec.ts.snap +248 -0
  28. package/src/stencil/ide/__snapshots__/ide.spec.ts.snap +11 -124
  29. package/src/stencil/ide/cem.spec.ts +35 -0
  30. package/src/stencil/ide/cem.ts +96 -0
  31. package/src/stencil/ide/ide.ts +33 -26
  32. package/src/stencil/ide/index.ts +1 -0
  33. package/src/stencil/index.ts +1 -0
  34. package/src/stencil/normalize-boolean-attributes/index.ts +1 -0
  35. package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.spec.ts +63 -0
  36. package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.ts +40 -0
  37. package/src/storybook/stencil/__snapshots__/stencil.spec.ts.snap +1 -1
@@ -0,0 +1,96 @@
1
+ import type { JsonDocs, JsonDocsComponent } from '@stencil/core/internal';
2
+
3
+ /**
4
+ * Convert a Stencil component's JsonDocs into a CEM v2 module entry.
5
+ * @param component - Stencil component doc
6
+ * @returns CEM module
7
+ */
8
+ const componentToModule = (component: JsonDocsComponent) => {
9
+ const className = component.tag
10
+ .split('-')
11
+ .map(part => part.charAt(0).toUpperCase() + part.slice(1))
12
+ .join('');
13
+
14
+ const attributes = component.props
15
+ .filter(prop => prop.attr !== undefined)
16
+ .map(prop => ({
17
+ name: prop.attr,
18
+ description: prop.docs,
19
+ type: { text: prop.type },
20
+ ...(prop.default !== undefined && { default: prop.default }),
21
+ fieldName: prop.name,
22
+ }));
23
+
24
+ const fieldMembers = component.props.map(prop => ({
25
+ kind: 'field' as const,
26
+ name: prop.name,
27
+ description: prop.docs,
28
+ type: { text: prop.type },
29
+ ...(prop.default !== undefined && { default: prop.default }),
30
+ ...(prop.attr !== undefined && { attribute: prop.attr }),
31
+ }));
32
+
33
+ const methodMembers = component.methods.map(method => ({
34
+ kind: 'method' as const,
35
+ name: method.name,
36
+ description: method.docs,
37
+ }));
38
+
39
+ return {
40
+ kind: 'javascript-module' as const,
41
+ path: component.filePath ?? '',
42
+ declarations: [
43
+ {
44
+ kind: 'class' as const,
45
+ name: className,
46
+ tagName: component.tag,
47
+ customElement: true,
48
+ description: component.overview ?? '',
49
+ attributes,
50
+ members: [...fieldMembers, ...methodMembers],
51
+ events: component.events.map(event => ({
52
+ name: event.event,
53
+ description: event.docs,
54
+ type: { text: `CustomEvent<${event.detail}>` },
55
+ })),
56
+ slots: component.slots.map(slot => ({
57
+ name: slot.name,
58
+ description: slot.docs,
59
+ })),
60
+ cssProperties: component.styles.map(style => ({
61
+ name: style.name,
62
+ description: style.docs,
63
+ })),
64
+ cssParts: component.parts.map(part => ({
65
+ name: part.name,
66
+ description: part.docs,
67
+ })),
68
+ },
69
+ ],
70
+ exports: [
71
+ {
72
+ kind: 'custom-element-definition' as const,
73
+ name: component.tag,
74
+ declaration: {
75
+ name: className,
76
+ module: component.filePath ?? '',
77
+ },
78
+ },
79
+ ],
80
+ };
81
+ };
82
+
83
+ /**
84
+ * Generate a Custom Elements Manifest v2 from Stencil JSON docs.
85
+ * @param jsonDocs - Stencil JSON doc
86
+ * @returns CEM v2
87
+ * @example
88
+ * ```ts
89
+ * const cem = cemGenerator(jsonDocs);
90
+ * ```
91
+ */
92
+ export const cemGenerator = (jsonDocs: JsonDocs) => ({
93
+ schemaVersion: '2.0.0',
94
+ readme: '',
95
+ modules: jsonDocs.components.map(componentToModule),
96
+ });
@@ -15,52 +15,55 @@ const getSourcesUrl = (sourcesBaseUrl: string, filePath: string | undefined): st
15
15
  };
16
16
 
17
17
  /**
18
- * Get Component element description
18
+ * Get Component element description.
19
+ *
20
+ * Neither WebStorm nor VS Code render the structured `attributes` /
21
+ * `js.properties` arrays in the tag-level quick-doc — they only show this
22
+ * markdown description. So the attribute and property listings have to live
23
+ * here, even though they're redundant with the structured arrays used by the
24
+ * inline autocomplete.
19
25
  * @param component - Component
20
26
  * @returns Component element description
21
27
  */
22
28
  const getElementDescription = (component: JsonDocsComponent): string => {
23
- // Init description
24
29
  let description = component.overview ? `${component.overview}\n\n` : '';
25
- // Attributes
26
30
  const attributes = component.props.filter(({ attr }) => attr !== undefined);
27
31
  if (attributes.length) {
28
32
  description += `Attributes:\n`;
29
33
  description += attributes.map(({ attr, docs }) => `- \`${attr}\`: ${docs}\n`).join('');
30
34
  description += '\n';
31
35
  }
32
- // Properties
33
36
  const properties = component.props.filter(({ attr }) => attr === undefined);
34
37
  if (properties.length) {
35
38
  description += `Properties:\n`;
36
39
  description += properties.map(({ name, docs }) => `- \`${name}\`: ${docs}\n`).join('');
37
40
  description += '\n';
38
41
  }
39
- // Methods
40
42
  if (component.methods.length) {
41
43
  description += `Methods:\n`;
42
44
  description += component.methods.map(({ name, docs }) => `- \`${name}\`: ${docs}\n`).join('');
43
45
  description += '\n';
44
46
  }
45
- // Events
46
47
  if (component.events.length) {
47
48
  description += `Events:\n`;
48
49
  description += component.events.map(({ event, docs }) => `- \`${event}\`: ${docs}\n`).join('');
49
50
  description += '\n';
50
51
  }
51
- // Listeners
52
52
  if (component.listeners.length) {
53
53
  description += `Listeners:\n`;
54
54
  description += component.listeners.map(({ event }) => `- \`${event}\`\n`).join('');
55
55
  description += '\n';
56
56
  }
57
- // Slots
58
57
  if (component.slots.length) {
59
58
  description += `Slots:\n`;
60
- description += component.slots.map(({ name, docs }) => `- \`${name}\`: ${docs}\n`).join('');
59
+ description += component.slots
60
+ .map(({ name, docs }) => {
61
+ const label = name ? `\`${name}\`` : 'default';
62
+ return `- ${label}: ${docs}\n`;
63
+ })
64
+ .join('');
61
65
  description += '\n';
62
66
  }
63
- // Return
64
67
  return description;
65
68
  };
66
69
 
@@ -111,16 +114,18 @@ export const webTypesGenerator = (name: string, version: string, jsonDocs: JsonD
111
114
  },
112
115
  })),
113
116
  'js': {
114
- properties: component.props.map(prop => ({
115
- 'name': prop.name,
116
- 'description': getAttributeDescription(prop),
117
- 'doc-url': docUrl,
118
- 'value': {
119
- type: prop.type,
120
- default: prop.default,
121
- required: prop.required,
122
- },
123
- })),
117
+ properties: component.props
118
+ .filter(prop => prop.attr === undefined)
119
+ .map(prop => ({
120
+ 'name': prop.name,
121
+ 'description': getAttributeDescription(prop),
122
+ 'doc-url': docUrl,
123
+ 'value': {
124
+ type: prop.type,
125
+ default: prop.default,
126
+ required: prop.required,
127
+ },
128
+ })),
124
129
  events: component.events.map(event => ({
125
130
  name: event.event,
126
131
  description: event.docs,
@@ -181,12 +186,14 @@ export const vsCodeGenerator = (jsonDocs: JsonDocs, storybookBaseUrl: string, so
181
186
  return {
182
187
  name: component.tag,
183
188
  description: getElementDescription(component),
184
- attributes: component.props.map(prop => ({
185
- name: prop.attr ?? prop.name,
186
- description: getAttributeDescription(prop),
187
- values: getValues(prop),
188
- references,
189
- })),
189
+ attributes: component.props
190
+ .filter(prop => prop.attr !== undefined)
191
+ .map(prop => ({
192
+ name: prop.attr,
193
+ description: getAttributeDescription(prop),
194
+ values: getValues(prop),
195
+ references,
196
+ })),
190
197
  references,
191
198
  };
192
199
  }),
@@ -1 +1,2 @@
1
1
  export * from './ide';
2
+ export { cemGenerator } from './cem';
@@ -1 +1,2 @@
1
1
  export * from './ide';
2
+ export * from './normalize-boolean-attributes';
@@ -0,0 +1 @@
1
+ export * from './normalize-boolean-attributes';
@@ -0,0 +1,63 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { normalizeBooleanAttributes } from '.';
3
+
4
+ const makeTarget = (html: string, props: Record<string, unknown>): { element: HTMLElement } => {
5
+ const element = document.createElement('div');
6
+ // Parse attributes from a fake tag so we can pass arbitrary attribute names/values
7
+ const tmp = document.createElement('div');
8
+ tmp.innerHTML = `<my-elem ${html}></my-elem>`;
9
+ const source = tmp.firstElementChild as HTMLElement;
10
+ for (const attr of Array.from(source.attributes)) {
11
+ element.setAttribute(attr.name, attr.value);
12
+ }
13
+ return { element, ...props };
14
+ };
15
+
16
+ describe('normalizeBooleanAttributes', () => {
17
+ test('rewrites boolean-typed attributes to empty string regardless of original value', () => {
18
+ const target = makeTarget('readonly="false" disabled="true" required="" multiple', {
19
+ readonly: false,
20
+ disabled: true,
21
+ required: false,
22
+ multiple: false,
23
+ });
24
+ normalizeBooleanAttributes(target);
25
+ expect(target.element.getAttribute('readonly')).toBe('');
26
+ expect(target.element.getAttribute('disabled')).toBe('');
27
+ expect(target.element.getAttribute('required')).toBe('');
28
+ expect(target.element.getAttribute('multiple')).toBe('');
29
+ });
30
+
31
+ test('skips attributes that do not map to a boolean prop', () => {
32
+ const target = makeTarget('class="foo" id="bar" data-x="1"', {});
33
+ normalizeBooleanAttributes(target);
34
+ expect(target.element.getAttribute('class')).toBe('foo');
35
+ expect(target.element.getAttribute('id')).toBe('bar');
36
+ expect(target.element.getAttribute('data-x')).toBe('1');
37
+ });
38
+
39
+ test('skips attributes whose prop is not boolean-typed', () => {
40
+ const target = makeTarget('label="hello" count="3"', { label: 'hello', count: 3 });
41
+ normalizeBooleanAttributes(target);
42
+ expect(target.element.getAttribute('label')).toBe('hello');
43
+ expect(target.element.getAttribute('count')).toBe('3');
44
+ });
45
+
46
+ test('converts kebab-case attribute name to camelCase prop name', () => {
47
+ const target = makeTarget('is-legend="false" label-on-top="false"', { isLegend: false, labelOnTop: false });
48
+ normalizeBooleanAttributes(target);
49
+ expect(target.element.getAttribute('is-legend')).toBe('');
50
+ expect(target.element.getAttribute('label-on-top')).toBe('');
51
+ });
52
+
53
+ test('leaves attributes alone when there is no matching prop', () => {
54
+ const target = makeTarget('flag="false"', {});
55
+ normalizeBooleanAttributes(target);
56
+ expect(target.element.getAttribute('flag')).toBe('false');
57
+ });
58
+
59
+ test('handles an element with no attributes', () => {
60
+ const target = makeTarget('', { readonly: false });
61
+ expect(() => normalizeBooleanAttributes(target)).not.toThrow();
62
+ });
63
+ });
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Per the HTML spec, a boolean attribute is `true` whenever it is present on the
3
+ * element, regardless of its value (including `="false"`). Stencil's default Prop
4
+ * parser converts the string `"false"` to the boolean `false`, which breaks this
5
+ * contract for consumers writing markup. Call this helper from `componentWillLoad`
6
+ * to re-normalize every present attribute that maps to a boolean-typed Prop.
7
+ *
8
+ * Iterates the host element's attributes and, for each one whose camelCase name
9
+ * matches a `typeof === 'boolean'` Prop on the instance, rewrites the attribute
10
+ * to `''` — Stencil's parser then turns that into `true` via the standard
11
+ * attribute → prop pipeline (no need for `mutable: true` on the Prop, since the
12
+ * write goes through Stencil's internal setter, not through user code).
13
+ *
14
+ * Non-boolean Props and non-Prop attributes (`class`, `id`, ...) are skipped
15
+ * naturally by the type check.
16
+ *
17
+ * @param target - the component instance (must expose `element` via `@Element()`)
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * @Component({ tag: 'my-input' })
22
+ * export class MyInput {
23
+ * @Element() element: HTMLMyInputElement;
24
+ * @Prop() readonly = false;
25
+ * @Prop() disabled = false;
26
+ *
27
+ * componentWillLoad() {
28
+ * normalizeBooleanAttributes(this);
29
+ * }
30
+ * }
31
+ * ```
32
+ */
33
+ export const normalizeBooleanAttributes = (target: { element: HTMLElement }): void => {
34
+ for (const attr of Array.from(target.element.attributes)) {
35
+ const propName = attr.name.replace(/-([a-z])/g, (_, c: string) => c.toUpperCase());
36
+ if (typeof (target as Record<string, unknown>)[propName] === 'boolean') {
37
+ target.element.setAttribute(attr.name, '');
38
+ }
39
+ }
40
+ };
@@ -72,7 +72,7 @@ exports[`stencil > StorybookPreview > extractArgTypes > Should extract arg types
72
72
  "table": {
73
73
  "category": "props",
74
74
  "defaultValue": {
75
- "summary": undefined,
75
+ "summary": "false",
76
76
  },
77
77
  "type": {
78
78
  "summary": "boolean",