jsx-framework-test-pb 0.0.2 → 0.0.4

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,4 +1,4 @@
1
- export type { Element, ElementProps, ElementChild, DevElement } from './types';
2
- export declare function isElement(value: any): value is Element;
3
- export declare function render(element: any, container: HTMLElement): void;
4
- import type { Element } from './types';
1
+ export { render } from './runtime';
2
+ export { Fragment } from './jsx-runtime';
3
+ export { isElement } from './utils';
4
+ export type { Element, ElementProps, ElementChild, DevElement, FC, HTMLAttributes, CSSProperties } from './types';
package/dist/index.js CHANGED
@@ -1,69 +1,3 @@
1
- const ELEMENT_TYPE = Symbol.for('your.framework.element');
2
- export function isElement(value) {
3
- return value && value.$$typeof === ELEMENT_TYPE;
4
- }
5
- export function render(element, container) {
6
- container.innerHTML = '';
7
- const domNode = createElement(element);
8
- if (domNode) {
9
- container.appendChild(domNode);
10
- }
11
- }
12
- function createElement(element) {
13
- if (element === null ||
14
- element === undefined ||
15
- typeof element === 'boolean') {
16
- return null;
17
- }
18
- if (typeof element === 'string' || typeof element === 'number') {
19
- return document.createTextNode(String(element));
20
- }
21
- if (Array.isArray(element)) {
22
- const fragment = document.createDocumentFragment();
23
- element.forEach(child => {
24
- const node = createElement(child);
25
- if (node)
26
- fragment.appendChild(node);
27
- });
28
- return fragment;
29
- }
30
- if (!isElement(element)) {
31
- return null;
32
- }
33
- const { type, props } = element;
34
- if (typeof type === 'function') {
35
- const result = type(props);
36
- return createElement(result);
37
- }
38
- if (typeof type === 'string') {
39
- const domElement = document.createElement(type);
40
- Object.keys(props).forEach(key => {
41
- if (key === 'children') {
42
- const children = Array.isArray(props.children)
43
- ? props.children
44
- : [props.children];
45
- children.forEach(child => {
46
- const childNode = createElement(child);
47
- if (childNode) {
48
- domElement.appendChild(childNode);
49
- }
50
- });
51
- }
52
- else if (key.startsWith('on') && typeof props[key] === 'function') {
53
- const eventName = key.substring(2).toLowerCase();
54
- domElement.addEventListener(eventName, props[key]);
55
- }
56
- else if (key === 'className') {
57
- domElement.setAttribute('class', props[key]);
58
- }
59
- else if (key === 'style' && typeof props[key] === 'object') {
60
- Object.assign(domElement.style, props[key]);
61
- }
62
- else if (key !== 'key') {
63
- domElement.setAttribute(key, props[key]);
64
- }
65
- });
66
- return domElement;
67
- }
68
- return null;
69
- }
1
+ export { render } from './runtime';
2
+ export { Fragment } from './jsx-runtime';
3
+ export { isElement } from './utils';
@@ -1,5 +1,6 @@
1
1
  import type { DevElement, ElementProps } from './types';
2
- export declare function jsxDEV(type: string | Function, props: ElementProps, key?: string, isStaticChildren?: boolean, source?: {
2
+ export declare const Fragment: symbol;
3
+ export declare function jsxDEV(type: string | Function | symbol, props: ElementProps, key?: string, isStaticChildren?: boolean, source?: {
3
4
  fileName: string;
4
5
  lineNumber: number;
5
6
  columnNumber: number;
@@ -1,4 +1,5 @@
1
- const ELEMENT_TYPE = Symbol.for('your.framework.element');
1
+ import { ELEMENT_TYPE, FRAGMENT_TYPE } from './utils';
2
+ export const Fragment = FRAGMENT_TYPE;
2
3
  export function jsxDEV(type, props, key, isStaticChildren, source, self) {
3
4
  return {
4
5
  $$typeof: ELEMENT_TYPE,
@@ -1,3 +1,4 @@
1
1
  import type { Element, ElementProps } from './types';
2
- export declare function jsx(type: string | Function, props: ElementProps, key?: string): Element;
2
+ export declare const Fragment: symbol;
3
+ export declare function jsx(type: string | Function | symbol, props: ElementProps, key?: string): Element;
3
4
  export declare const jsxs: typeof jsx;
@@ -1,4 +1,5 @@
1
- const ELEMENT_TYPE = Symbol.for('your.framework.element');
1
+ import { ELEMENT_TYPE, FRAGMENT_TYPE } from './utils';
2
+ export const Fragment = FRAGMENT_TYPE;
2
3
  export function jsx(type, props, key) {
3
4
  return {
4
5
  $$typeof: ELEMENT_TYPE,
@@ -0,0 +1 @@
1
+ export declare function createElement(element: any): Node | null;
@@ -0,0 +1,78 @@
1
+ import { isElement, isValidChild, FRAGMENT_TYPE } from '../utils';
2
+ export function createElement(element) {
3
+ if (!isValidChild(element)) {
4
+ return null;
5
+ }
6
+ if (typeof element === 'string' || typeof element === 'number') {
7
+ return document.createTextNode(String(element));
8
+ }
9
+ if (Array.isArray(element)) {
10
+ return createFragment(element);
11
+ }
12
+ if (!isElement(element)) {
13
+ return null;
14
+ }
15
+ const { type, props } = element;
16
+ if (type === FRAGMENT_TYPE) {
17
+ return createFragment(getChildren(props));
18
+ }
19
+ if (typeof type === 'function') {
20
+ const result = type(props);
21
+ return createElement(result);
22
+ }
23
+ if (typeof type === 'string') {
24
+ return createHTMLElement(type, props);
25
+ }
26
+ return null;
27
+ }
28
+ function createFragment(children) {
29
+ const fragment = document.createDocumentFragment();
30
+ const childArray = Array.isArray(children) ? children : [children];
31
+ childArray.forEach(child => {
32
+ const node = createElement(child);
33
+ if (node)
34
+ fragment.appendChild(node);
35
+ });
36
+ return fragment;
37
+ }
38
+ function getChildren(props) {
39
+ const children = props.children;
40
+ return Array.isArray(children) ? children : children ? [children] : [];
41
+ }
42
+ function createHTMLElement(type, props) {
43
+ const element = document.createElement(type);
44
+ Object.keys(props).forEach(key => {
45
+ if (key === 'children') {
46
+ appendChildren(element, props.children);
47
+ }
48
+ else if (key.startsWith('on') && typeof props[key] === 'function') {
49
+ attachEvent(element, key, props[key]);
50
+ }
51
+ else if (key === 'className') {
52
+ element.setAttribute('class', props[key]);
53
+ }
54
+ else if (key === 'style' && typeof props[key] === 'object') {
55
+ applyStyles(element, props[key]);
56
+ }
57
+ else if (key !== 'key' && props[key] != null) {
58
+ element.setAttribute(key, String(props[key]));
59
+ }
60
+ });
61
+ return element;
62
+ }
63
+ function appendChildren(parent, children) {
64
+ const childArray = Array.isArray(children) ? children : [children];
65
+ childArray.forEach(child => {
66
+ const childNode = createElement(child);
67
+ if (childNode) {
68
+ parent.appendChild(childNode);
69
+ }
70
+ });
71
+ }
72
+ function attachEvent(element, key, handler) {
73
+ const eventName = key.substring(2).toLowerCase();
74
+ element.addEventListener(eventName, handler);
75
+ }
76
+ function applyStyles(element, styles) {
77
+ Object.assign(element.style, styles);
78
+ }
@@ -0,0 +1,2 @@
1
+ export { createElement } from './createElement';
2
+ export { render } from './render';
@@ -0,0 +1,2 @@
1
+ export { createElement } from './createElement';
2
+ export { render } from './render';
@@ -0,0 +1 @@
1
+ export declare function render(element: any, container: HTMLElement): void;
@@ -0,0 +1,11 @@
1
+ import { createElement } from './createElement';
2
+ export function render(element, container) {
3
+ if (!container) {
4
+ throw new Error('Container element is required');
5
+ }
6
+ container.innerHTML = '';
7
+ const domNode = createElement(element);
8
+ if (domNode) {
9
+ container.appendChild(domNode);
10
+ }
11
+ }
package/dist/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export interface Element {
2
2
  $$typeof: symbol;
3
- type: string | Function;
3
+ type: string | Function | symbol;
4
4
  props: ElementProps;
5
5
  key: string | null;
6
6
  }
@@ -17,3 +17,14 @@ export interface ElementProps {
17
17
  [key: string]: any;
18
18
  }
19
19
  export type ElementChild = Element | string | number | boolean | null | undefined;
20
+ export type FC<P = {}> = (props: P) => Element | null;
21
+ export interface CSSProperties {
22
+ [key: string]: string | number;
23
+ }
24
+ export interface HTMLAttributes<T = HTMLElement> {
25
+ className?: string;
26
+ id?: string;
27
+ style?: CSSProperties;
28
+ children?: ElementChild | ElementChild[];
29
+ [key: string]: any;
30
+ }
@@ -0,0 +1,2 @@
1
+ export declare const ELEMENT_TYPE: unique symbol;
2
+ export declare const FRAGMENT_TYPE: unique symbol;
@@ -0,0 +1,2 @@
1
+ export const ELEMENT_TYPE = Symbol.for('your.framework.element');
2
+ export const FRAGMENT_TYPE = Symbol.for('your.framework.fragment');
@@ -0,0 +1,3 @@
1
+ import type { Element } from '../types';
2
+ export declare function isElement(value: any): value is Element;
3
+ export declare function isValidChild(element: any): boolean;
@@ -0,0 +1,9 @@
1
+ import { ELEMENT_TYPE } from './constants';
2
+ export function isElement(value) {
3
+ return value && value.$$typeof === ELEMENT_TYPE;
4
+ }
5
+ export function isValidChild(element) {
6
+ return element !== null &&
7
+ element !== undefined &&
8
+ typeof element !== 'boolean';
9
+ }
@@ -0,0 +1,2 @@
1
+ export { ELEMENT_TYPE, FRAGMENT_TYPE } from './constants';
2
+ export { isElement, isValidChild } from './guards';
@@ -0,0 +1,2 @@
1
+ export { ELEMENT_TYPE, FRAGMENT_TYPE } from './constants';
2
+ export { isElement, isValidChild } from './guards';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsx-framework-test-pb",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",