From 4f34b2f55fefe022a4b741571e997dc657368bb0 Mon Sep 17 00:00:00 2001
From: Christopher J Baker <christopher@bitovi.com>
Date: Wed, 25 Oct 2023 10:35:16 -0700
Subject: [PATCH 1/4] misc cleanup

---
 package.json                                 |  3 +-
 packages/core/package.json                   |  2 +-
 packages/core/src/core.ts                    | 37 +++++++++-----------
 packages/core/src/transforms/boolean.ts      |  4 +--
 packages/core/src/transforms/function.ts     |  6 ++--
 packages/core/src/transforms/index.ts        |  4 +--
 packages/core/src/transforms/json.ts         |  4 +--
 packages/core/src/transforms/number.ts       |  4 +--
 packages/core/src/utils.ts                   | 10 ++++++
 packages/legacy/README.md                    |  2 +-
 packages/legacy/package.json                 |  2 +-
 packages/react-to-web-component/package.json |  2 +-
 tsconfig.json                                |  1 +
 13 files changed, 44 insertions(+), 37 deletions(-)
 create mode 100644 packages/core/src/utils.ts

diff --git a/package.json b/package.json
index 04064f8..1963749 100644
--- a/package.json
+++ b/package.json
@@ -1,9 +1,10 @@
 {
+  "private": true,
+  "homepage": "https://www.bitovi.com/open-source/react-to-web-component",
   "namespace": "r2wc",
   "workspaces": [
     "packages/*"
   ],
-  "private": true,
   "scripts": {
     "check-packages": "ts-node ./scripts/check-packages"
   },
diff --git a/packages/core/package.json b/packages/core/package.json
index a7d348f..e81add2 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -2,7 +2,7 @@
   "name": "@r2wc/core",
   "version": "1.0.1",
   "description": "Convert framework components to native Web Components.",
-  "homepage": "https://www.bitovi.com/frontend-javascript-consulting/react-consulting",
+  "homepage": "https://www.bitovi.com/open-source/react-to-web-component",
   "author": "Bitovi",
   "license": "MIT",
   "keywords": [
diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts
index 65ef281..7c0460e 100644
--- a/packages/core/src/core.ts
+++ b/packages/core/src/core.ts
@@ -1,8 +1,9 @@
 import type { R2WCType } from "./transforms"
 
 import transforms from "./transforms"
+import { toDashedCase } from "./utils"
 
-type PropName<Props> = Extract<keyof Props, string>
+type PropName<Props> = Exclude<Extract<keyof Props, string>, "container">
 type PropNames<Props> = Array<PropName<Props>>
 
 export interface R2WCOptions<Props> {
@@ -20,6 +21,10 @@ export interface R2WCRenderer<Props, Context> {
   unmount: (context: Context) => void
 }
 
+export interface R2WCBaseProps {
+  container?: HTMLElement
+}
+
 const renderSymbol = Symbol.for("r2wc.render")
 const connectedSymbol = Symbol.for("r2wc.connected")
 const contextSymbol = Symbol.for("r2wc.context")
@@ -32,7 +37,7 @@ const propsSymbol = Symbol.for("r2wc.props")
  * @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>(
+export default function r2wc<Props extends R2WCBaseProps, Context>(
   ReactComponent: React.ComponentType<Props>,
   options: R2WCOptions<Props>,
   renderer: R2WCRenderer<Props, Context>,
@@ -43,14 +48,9 @@ export default function r2wc<Props, Context>(
       : []
   }
 
-  const propNames = (
-    Array.isArray(options.props)
-      ? options.props.slice()
-      : (Object.keys(options.props) as PropNames<Props>)
-  ).filter((prop) => {
-    //@ts-ignore
-    return prop !== "container"
-  })
+  const propNames = Array.isArray(options.props)
+    ? options.props.slice()
+    : (Object.keys(options.props) as PropNames<Props>)
 
   const propTypes = {} as Record<PropName<Props>, R2WCType>
   const mapPropAttribute = {} as Record<PropName<Props>, string>
@@ -60,7 +60,7 @@ export default function r2wc<Props, Context>(
       ? "string"
       : options.props[prop]
 
-    const attribute = toDashedStyle(prop)
+    const attribute = toDashedCase(prop)
 
     mapPropAttribute[prop] = attribute
     mapAttributeProp[attribute] = prop
@@ -87,7 +87,6 @@ export default function r2wc<Props, Context>(
         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) {
@@ -96,9 +95,9 @@ export default function r2wc<Props, Context>(
         const type = propTypes[prop]
         const transform = transforms[type]
 
-        if (value && transform?.parse) {
+        if (transform?.parse && value) {
           //@ts-ignore
-          this[propsSymbol][prop] = transform.parse(value, this)
+          this[propsSymbol][prop] = transform.parse(value, attribute, this)
         }
       }
     }
@@ -126,9 +125,9 @@ export default function r2wc<Props, Context>(
       const type = propTypes[prop]
       const transform = transforms[type]
 
-      if (prop in propTypes && transform?.parse) {
+      if (prop in propTypes && transform?.parse && value) {
         //@ts-ignore
-        this[propsSymbol][prop] = transform.parse(value, this)
+        this[propsSymbol][prop] = transform.parse(value, attribute, this)
 
         this[renderSymbol]()
       }
@@ -165,7 +164,7 @@ export default function r2wc<Props, Context>(
         const transform = transforms[type]
         if (transform?.stringify) {
           //@ts-ignore
-          const attributeValue = transform.stringify(value)
+          const attributeValue = transform.stringify(value, attribute, this)
           const oldAttributeValue = this.getAttribute(attribute)
 
           if (oldAttributeValue !== attributeValue) {
@@ -178,7 +177,3 @@ export default function r2wc<Props, Context>(
 
   return ReactWebComponent
 }
-
-function toDashedStyle(camelCase = "") {
-  return camelCase.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase()
-}
diff --git a/packages/core/src/transforms/boolean.ts b/packages/core/src/transforms/boolean.ts
index 11d0a4b..10f4ba7 100644
--- a/packages/core/src/transforms/boolean.ts
+++ b/packages/core/src/transforms/boolean.ts
@@ -1,8 +1,8 @@
 import type { Transform } from "./index"
 
-const string: Transform<boolean> = {
+const boolean: Transform<boolean> = {
   stringify: (value) => (value ? "true" : "false"),
   parse: (value) => /^[ty1-9]/i.test(value),
 }
 
-export default string
+export default boolean
diff --git a/packages/core/src/transforms/function.ts b/packages/core/src/transforms/function.ts
index 966892f..f30bbef 100644
--- a/packages/core/src/transforms/function.ts
+++ b/packages/core/src/transforms/function.ts
@@ -1,8 +1,8 @@
 import type { Transform } from "./index"
 
-const string: Transform<(...args: unknown[]) => unknown> = {
+const function_: Transform<(...args: unknown[]) => unknown> = {
   stringify: (value) => value.name,
-  parse: (value, element) => {
+  parse: (value, attribute, element) => {
     const fn = (() => {
       if (typeof window !== "undefined" && value in window) {
         // @ts-expect-error
@@ -19,4 +19,4 @@ const string: Transform<(...args: unknown[]) => unknown> = {
   },
 }
 
-export default string
+export default function_
diff --git a/packages/core/src/transforms/index.ts b/packages/core/src/transforms/index.ts
index 6dccd76..491d06a 100644
--- a/packages/core/src/transforms/index.ts
+++ b/packages/core/src/transforms/index.ts
@@ -5,8 +5,8 @@ import function_ from "./function"
 import json from "./json"
 
 export interface Transform<Type> {
-  stringify?: (value: Type) => string
-  parse: (value: string, element: HTMLElement) => Type
+  stringify?: (value: Type, attribute: string, element: HTMLElement) => string
+  parse: (value: string, attribute: string, element: HTMLElement) => Type
 }
 
 const transforms = {
diff --git a/packages/core/src/transforms/json.ts b/packages/core/src/transforms/json.ts
index 3879339..63a938a 100644
--- a/packages/core/src/transforms/json.ts
+++ b/packages/core/src/transforms/json.ts
@@ -1,8 +1,8 @@
 import type { Transform } from "./index"
 
-const string: Transform<string> = {
+const json: Transform<string> = {
   stringify: (value) => JSON.stringify(value),
   parse: (value) => JSON.parse(value),
 }
 
-export default string
+export default json
diff --git a/packages/core/src/transforms/number.ts b/packages/core/src/transforms/number.ts
index 28c291e..51abf1c 100644
--- a/packages/core/src/transforms/number.ts
+++ b/packages/core/src/transforms/number.ts
@@ -1,8 +1,8 @@
 import type { Transform } from "./index"
 
-const string: Transform<number> = {
+const number: Transform<number> = {
   stringify: (value) => `${value}`,
   parse: (value) => parseFloat(value),
 }
 
-export default string
+export default number
diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts
new file mode 100644
index 0000000..72c6ca8
--- /dev/null
+++ b/packages/core/src/utils.ts
@@ -0,0 +1,10 @@
+export function toDashedCase(camelCase: string): string {
+  return camelCase.replace(
+    /([a-z0-9])([A-Z])/g,
+    (_, a, b) => `${a}-${b.toLowerCase()}`,
+  )
+}
+
+export function toCamelCase(dashedCase: string): string {
+  return dashedCase.replace(/[-:]([a-z])/g, (_, b) => `${b.toUpperCase()}`)
+}
diff --git a/packages/legacy/README.md b/packages/legacy/README.md
index 87a35ba..e7c876e 100644
--- a/packages/legacy/README.md
+++ b/packages/legacy/README.md
@@ -2,7 +2,7 @@
 
 `react-to-webcomponent` converts [React](https://reactjs.org/) components to [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements)! It lets you share React components as native elements that **don't** require mounted being through React. The custom element acts as a wrapper for the underlying React component. Use these custom elements with any project that uses HTML even in any framework (vue, svelte, angular, ember, canjs) the same way you would use standard HTML elements.
 
-> Note: This is a compatibility wrapper around our new, simpler API. We highly reccomend using the new [@r2wc/react-to-web-component](https://github.com/bitovi/react-to-web-component) package.
+> Note: This is a compatibility wrapper around our new, simpler API. We highly reccomend using the new [@r2wc/react-to-web-component](https://www.npmjs.com/package/@r2wc/react-to-web-component) package.
 
 `react-to-webcomponent`:
 
diff --git a/packages/legacy/package.json b/packages/legacy/package.json
index b01687a..6dea48d 100644
--- a/packages/legacy/package.json
+++ b/packages/legacy/package.json
@@ -2,7 +2,7 @@
   "name": "react-to-webcomponent",
   "version": "2.0.0",
   "description": "Convert React components to native Web Components.",
-  "homepage": "https://www.bitovi.com/frontend-javascript-consulting/react-consulting",
+  "homepage": "https://www.bitovi.com/open-source/react-to-web-component",
   "author": "Bitovi",
   "license": "MIT",
   "keywords": [
diff --git a/packages/react-to-web-component/package.json b/packages/react-to-web-component/package.json
index 49f70bd..f47a99c 100644
--- a/packages/react-to-web-component/package.json
+++ b/packages/react-to-web-component/package.json
@@ -2,7 +2,7 @@
   "name": "@r2wc/react-to-web-component",
   "version": "2.0.3",
   "description": "Convert React components to native Web Components.",
-  "homepage": "https://www.bitovi.com/frontend-javascript-consulting/react-consulting",
+  "homepage": "https://www.bitovi.com/open-source/react-to-web-component",
   "author": "Bitovi",
   "license": "MIT",
   "keywords": [
diff --git a/tsconfig.json b/tsconfig.json
index 38350bd..1e76085 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -9,6 +9,7 @@
     "composite": true,
     "incremental": true,
     "strict": true,
+
     "allowJs": false,
     "allowSyntheticDefaultImports": true,
     "downlevelIteration": true,

From c1a0f3540d4ef050d7e64fcf396051911a20979a Mon Sep 17 00:00:00 2001
From: Christopher J Baker <christopher@bitovi.com>
Date: Sat, 18 Nov 2023 03:46:18 -0800
Subject: [PATCH 2/4] update readmes

---
 packages/core/README.md                   | 117 ++++++++++++++++++++++
 packages/react-to-web-component/README.md |  10 +-
 2 files changed, 122 insertions(+), 5 deletions(-)
 create mode 100755 packages/core/README.md

diff --git a/packages/core/README.md b/packages/core/README.md
new file mode 100755
index 0000000..93356a3
--- /dev/null
+++ b/packages/core/README.md
@@ -0,0 +1,117 @@
+# React to Web Component
+
+!!!!!!!! FIX ME !!!!!!!!
+
+`react-to-webcomponent` converts [React](https://reactjs.org/) components to [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements)! It lets you share React components as native elements that **don't** require mounted being through React. The custom element acts as a wrapper for the underlying React component. Use these custom elements with any project that uses HTML even in any framework (vue, svelte, angular, ember, canjs) the same way you would use standard HTML elements.
+
+> Note: This is a compatibility wrapper around our new, simpler API. We highly reccomend using the new [@r2wc/react-to-web-component](https://github.com/bitovi/react-to-web-component) package.
+
+`react-to-webcomponent`:
+
+- Works in all modern browsers. (Edge needs a [customElements polyfill](https://github.com/webcomponents/polyfills/tree/master/packages/custom-elements)).
+- Is `1.37KB` minified and gzipped.
+
+## Need help or have questions?
+
+This project is supported by [Bitovi, a React consultancy](https://www.bitovi.com/frontend-javascript-consulting/react-consulting). You can get help or ask questions on our:
+
+- [Discord Community](https://discord.gg/J7ejFsZnJ4)
+- [Twitter](https://twitter.com/bitovi)
+
+Or, you can hire us for training, consulting, or development. [Set up a free consultation.](https://www.bitovi.com/frontend-javascript-consulting/react-consulting)
+
+## Basic Use
+
+For basic usage, we will use this simple React component:
+
+```js
+const Greeting = () => {
+  return <h1>Hello, World!</h1>
+}
+```
+
+With our React component complete, all we have to do is call `r2wc` and [customElements.define](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define) to create and define our custom element:
+
+```js
+import React from "react"
+import ReactDOM from "react-dom/client" // if using React 18
+// import ReactDOM from "react-dom" // if using React 17
+
+import r2wc from "react-to-webcomponent"
+
+const WebGreeting = r2wc(Greeting, React, ReactDOM)
+
+customElements.define("web-greeting", WebGreeting)
+```
+
+Now we can use `<web-greeting>` like any other HTML element!
+
+```html
+<body>
+  <h1>Greeting Demo</h1>
+
+  <web-greeting></web-greeting>
+</body>
+```
+
+In the above case, the web-greeting custom element is not making use of the `name` property from our `Greeting` component.
+
+## Working with Attributes
+
+By default, custom elements created by `r2wc` only pass properties to the underlying React component. To make attributes work, you must specify your component's props.
+
+```js
+const Greeting = ({ name }) => {
+  return <h1>Hello, {name}!</h1>
+}
+
+const WebGreeting = r2wc(Greeting, React, ReactDOM, {
+  props: {
+    name: "string",
+  },
+})
+```
+
+Now `r2wc` will know to look for `name` attributes
+as follows:
+
+```html
+<body>
+  <h1>Greeting Demo</h1>
+
+  <web-greeting name="Justin"></web-greeting>
+</body>
+```
+
+For projects needing more advanced usage of the web components, see our [programatic usage and declarative demos](docs/programatic-usage.md).
+
+We also have a [complete example using a third party library](docs/complete-example.md).
+
+## Setup
+
+To install from npm:
+
+```
+npm install react-to-webcomponent
+```
+
+## Examples
+
+* [Greeting](https://codesandbox.io/s/greeting-legacy-8oopz3)
+* [All the Props](https://codesandbox.io/s/all-the-props-legacy-0zh6iv)
+
+## Blog Posts
+
+R2WC with Vite [View Post](https://www.bitovi.com/blog/react-everywhere-with-vite-and-react-to-webcomponent)
+
+R2WC with Create React App (CRA) [View Post](https://www.bitovi.com/blog/how-to-create-a-web-component-with-create-react-app)
+
+## How it works
+
+Check out our [full API documentation](https://github.com/bitovi/react-to-web-component/blob/main/docs/api.md).
+
+# We want to hear from you.
+
+Come chat with us about open source in our Bitovi community [Discord](https://discord.gg/J7ejFsZnJ4).
+
+See what we're up to by following us on [Twitter](https://twitter.com/bitovi).
diff --git a/packages/react-to-web-component/README.md b/packages/react-to-web-component/README.md
index 7e5b629..347f280 100644
--- a/packages/react-to-web-component/README.md
+++ b/packages/react-to-web-component/README.md
@@ -91,11 +91,11 @@ npm install @r2wc/react-to-web-component
 
 ## Examples
 
-* [Greeting](https://codesandbox.io/s/greeting-md5oih)
-* [All the Props](https://codesandbox.io/s/all-the-props-n8z5hv)
-* [Header Demo](https://codesandbox.io/s/example-header-blog-7k313l)
-* [MUI Button](https://codesandbox.io/s/example-mui-button-qwidh9)
-* [Checklist Demo](https://codesandbox.io/s/example-checklist-blog-y3nqwx)
+* [Hello World](https://codesandbox.io/s/hello-world-md5oih) - The quintessential software demo!
+* [All the Props](https://codesandbox.io/s/all-the-props-n8z5hv) - A demo of all the prop transform types that R2WC supports.
+* [Header Example](https://codesandbox.io/s/example-header-blog-7k313l) - An example reusable Header component.
+* [MUI Button](https://codesandbox.io/s/example-mui-button-qwidh9) - An example application using an MUI button with theme customization.
+* [Checklist Demo](https://codesandbox.io/s/example-checklist-blog-y3nqwx) - An example Checklist application.
 
 ## Blog Posts
 

From 04cd729652f702cc789662c3bc92e61e4395ea9d Mon Sep 17 00:00:00 2001
From: Christopher J Baker <christopher@bitovi.com>
Date: Sat, 18 Nov 2023 15:40:39 -0800
Subject: [PATCH 3/4] add experimentalChildren

---
 packages/core/src/core.ts          |  7 +++
 packages/core/src/parseChildren.ts | 75 ++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+)
 create mode 100755 packages/core/src/parseChildren.ts

diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts
index 7c0460e..5ed06ed 100644
--- a/packages/core/src/core.ts
+++ b/packages/core/src/core.ts
@@ -1,5 +1,6 @@
 import type { R2WCType } from "./transforms"
 
+import parseChildren from "./parseChildren"
 import transforms from "./transforms"
 import { toDashedCase } from "./utils"
 
@@ -9,6 +10,7 @@ type PropNames<Props> = Array<PropName<Props>>
 export interface R2WCOptions<Props> {
   shadow?: "open" | "closed"
   props?: PropNames<Props> | Record<PropName<Props>, R2WCType>
+  experimentalChildren?: boolean
 }
 
 export interface R2WCRenderer<Props, Context> {
@@ -22,6 +24,7 @@ export interface R2WCRenderer<Props, Context> {
 }
 
 export interface R2WCBaseProps {
+  children?: React.ReactNode
   container?: HTMLElement
 }
 
@@ -100,6 +103,10 @@ export default function r2wc<Props extends R2WCBaseProps, Context>(
           this[propsSymbol][prop] = transform.parse(value, attribute, this)
         }
       }
+
+      if (options.experimentalChildren) {
+        this[propsSymbol].children = parseChildren(this.childNodes)
+      }
     }
 
     connectedCallback() {
diff --git a/packages/core/src/parseChildren.ts b/packages/core/src/parseChildren.ts
new file mode 100755
index 0000000..bdc5eb0
--- /dev/null
+++ b/packages/core/src/parseChildren.ts
@@ -0,0 +1,75 @@
+import React from "react"
+
+import { toCamelCase } from "./utils"
+
+const ELEMENT_NODE = 1
+const TEXT_NODE = 3
+const COMMENT_NODE = 8
+
+export default function parseChildren(
+  input: NodeListOf<ChildNode>,
+): React.ReactNode[] {
+  const output: React.ReactNode[] = []
+
+  for (let index = 0; index < input.length; index++) {
+    const child = input[index]
+    output.push(parseChild(child, index))
+  }
+
+  return output
+}
+
+function parseChild(
+  input: ChildNode,
+  index: number,
+): React.ReactNode | undefined {
+  if (input.nodeType === TEXT_NODE || input.nodeType === COMMENT_NODE) {
+    return input.nodeValue
+  }
+
+  if (input.nodeType === ELEMENT_NODE) {
+    const node = input as HTMLElement
+    const nodeName = node.localName
+    const children = parseChildren(node.childNodes)
+
+    const props: Record<string, unknown> = { key: index }
+    for (let { name, value } of node.attributes) {
+      if (name === "class") name = "class-name"
+      if (name === "for") name = "html-for"
+      if (name === "colspan") name = "colSpan"
+      if (name === "rowspan") name = "rowSpan"
+      if (nodeName === "input" && name === "value") name = "default-value"
+      if (nodeName === "input" && name === "checked") name = "default-checked"
+
+      if (!name.startsWith("data-")) name = toCamelCase(name)
+
+      if (name === "style") {
+        const input = value
+          .split(";")
+          .filter((value) => value.length > 0)
+          .map((value) =>
+            value
+              .trim()
+              .split(":")
+              .map((value) => value.trim()),
+          )
+
+        const styles = {}
+        for (let [key, value] of input) {
+          key = toCamelCase(key)
+
+          styles[key] = value
+        }
+
+        // @ts-ignore
+        value = styles
+      }
+
+      props[name] = value
+    }
+
+    return React.createElement(nodeName, props, ...children)
+  }
+
+  return undefined
+}

From e613c5666fd21ce0978fcc9b94395d8f63367a28 Mon Sep 17 00:00:00 2001
From: Christopher J Baker <christopher@bitovi.com>
Date: Fri, 12 Jul 2024 15:04:34 -0700
Subject: [PATCH 4/4] fix

---
 packages/core/src/parseChildren.ts | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/packages/core/src/parseChildren.ts b/packages/core/src/parseChildren.ts
index bdc5eb0..0d9045d 100755
--- a/packages/core/src/parseChildren.ts
+++ b/packages/core/src/parseChildren.ts
@@ -55,10 +55,11 @@ function parseChild(
           )
 
         const styles = {}
-        for (let [key, value] of input) {
-          key = toCamelCase(key)
+        for (const [key, value] of input) {
+          const camelKey = toCamelCase(key)
 
-          styles[key] = value
+          // @ts-ignore
+          styles[camelKey] = value
         }
 
         // @ts-ignore