-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathcore.ts
191 lines (160 loc) · 5.19 KB
/
core.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import type { R2WCType } from "./transforms"
import transforms from "./transforms"
type PropName<Props> = Extract<keyof Props, string>
type PropNames<Props> = Array<PropName<Props>>
export interface R2WCOptions<Props> {
shadow?: "open" | "closed"
props?: PropNames<Props> | Record<PropName<Props>, R2WCType>
}
export interface R2WCRenderer<Props, Context> {
mount: (
container: HTMLElement,
ReactComponent: React.ComponentType<Props>,
props: Props,
) => Context
update: (context: Context, props: Props) => void
unmount: (context: Context) => void
}
const renderSymbol = Symbol.for("r2wc.render")
const connectedSymbol = Symbol.for("r2wc.connected")
const contextSymbol = Symbol.for("r2wc.context")
const propsSymbol = Symbol.for("r2wc.props")
/**
* Converts a React component into a Web Component.
* @param {ReactComponent}
* @param {Object} options - Optional parameters
* @param {String?} options.shadow - Shadow DOM mode as either open or closed.
* @param {Object|Array?} options.props - Array of camelCasedProps to watch as Strings or { [camelCasedProp]: "string" | "number" | "boolean" | "function" | "json" }
*/
export default function r2wc<Props, Context>(
ReactComponent: React.ComponentType<Props>,
options: R2WCOptions<Props>,
renderer: R2WCRenderer<Props, Context>,
): CustomElementConstructor {
if (!options.props) {
options.props = ReactComponent.propTypes
? (Object.keys(ReactComponent.propTypes) as PropNames<Props>)
: []
}
const propNames = (
Array.isArray(options.props)
? options.props.slice()
: (Object.keys(options.props) as PropNames<Props>)
).filter((prop) => {
//@ts-ignore
return prop !== "container"
})
const propTypes = {} as Record<PropName<Props>, R2WCType>
const mapPropAttribute = {} as Record<PropName<Props>, string>
const mapAttributeProp = {} as Record<string, PropName<Props>>
for (const prop of propNames) {
propTypes[prop] = Array.isArray(options.props)
? "string"
: options.props[prop]
const attribute = toDashedStyle(prop)
mapPropAttribute[prop] = attribute
mapAttributeProp[attribute] = prop
}
class ReactWebComponent extends HTMLElement {
static get observedAttributes() {
return Object.keys(mapAttributeProp)
}
[connectedSymbol] = true;
[contextSymbol]?: Context;
[propsSymbol]: Props = {} as Props
container: HTMLElement
constructor() {
super()
if (options.shadow) {
this.container = this.attachShadow({
mode: options.shadow,
}) as unknown as HTMLElement
} else {
this.container = this
}
// @ts-ignore: There won't always be a container in the definition
this[propsSymbol].container = this.container
for (const prop of propNames) {
const attribute = mapPropAttribute[prop]
const value = this.getAttribute(attribute)
const type = propTypes[prop]
const transform = transforms[type]
if (!value && type === "boolean") {
//@ts-ignore
this[propsSymbol][prop] = true
} else if (value && transform?.parse) {
//@ts-ignore
this[propsSymbol][prop] = transform.parse(value, attribute, this)
}
}
}
connectedCallback() {
this[connectedSymbol] = true
this[renderSymbol]()
}
disconnectedCallback() {
this[connectedSymbol] = false
if (this[contextSymbol]) {
renderer.unmount(this[contextSymbol])
}
}
attributeChangedCallback(
attribute: string,
oldValue: string,
value: string,
) {
const prop = mapAttributeProp[attribute]
const type = propTypes[prop]
const transform = transforms[type]
if (prop in propTypes && transform?.parse) {
if (!value && type === "boolean") {
//@ts-ignore
this[propsSymbol][prop] = true
} else {
//@ts-ignore
this[propsSymbol][prop] = transform.parse(value, attribute, this)
}
this[renderSymbol]()
}
}
[renderSymbol]() {
if (!this[connectedSymbol]) return
if (!this[contextSymbol]) {
this[contextSymbol] = renderer.mount(
this.container,
ReactComponent,
this[propsSymbol],
)
} else {
renderer.update(this[contextSymbol], this[propsSymbol])
}
}
}
for (const prop of propNames) {
const attribute = mapPropAttribute[prop]
const type = propTypes[prop]
Object.defineProperty(ReactWebComponent.prototype, prop, {
enumerable: true,
configurable: true,
get() {
return this[propsSymbol][prop]
},
set(value) {
this[propsSymbol][prop] = value
const transform = transforms[type]
if (transform?.stringify) {
//@ts-ignore
const attributeValue = transform.stringify(value)
const oldAttributeValue = this.getAttribute(attribute)
if (oldAttributeValue !== attributeValue) {
this.setAttribute(attribute, attributeValue)
}
}
},
})
}
return ReactWebComponent
}
function toDashedStyle(camelCase = "") {
return camelCase.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase()
}