Skip to content

Commit

Permalink
Merge pull request #45 from break-stuff/react-wrappers-attribute-mapping
Browse files Browse the repository at this point in the history
React wrappers attribute mapping
  • Loading branch information
break-stuff authored Dec 16, 2023
2 parents 108bd02 + da353eb commit 76a283b
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions demo/lit-app/custom-element-jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ type RadioButtonProps = {
disabled?: RadioButton["disabled"];
/** A lookup type for example */
target?: RadioButton["target"];
/** A mapped attribute for react wrapper example */
for?: RadioButton["for"];
};

export type CustomElements = {
Expand Down
2 changes: 2 additions & 0 deletions demo/lit-app/custom-element-vuejs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type RadioButtonProps = {
disabled?: RadioButton["disabled"];
/** A lookup type for example */
target?: RadioButton["target"];
/** A mapped attribute for react wrapper example */
for?: RadioButton["for"];
};

export type CustomElements = {
Expand Down
3 changes: 3 additions & 0 deletions demo/lit-app/custom-elements-manifest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export default {
type: "React.FocusEventHandler",
},
],
attributeMapping: {
"for": "_for",
}
}),
customElementVuejsPlugin({
componentTypePath: (name, tag) => `./dist/${tag}/${name}.d.ts`,
Expand Down
17 changes: 17 additions & 0 deletions demo/lit-app/custom-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,15 @@
"expandedType": {
"text": "'_blank' | '_self' | '_parent' | '_top' | undefined"
}
},
{
"kind": "field",
"name": "for",
"type": {
"text": "string | undefined"
},
"description": "A mapped attribute for react wrapper example",
"attribute": "for"
}
],
"attributes": [
Expand Down Expand Up @@ -460,6 +469,14 @@
"text": "'_blank' | '_self' | '_parent' | '_top' | undefined"
},
"propName": "target"
},
{
"name": "for",
"type": {
"text": "string | undefined"
},
"description": "A mapped attribute for react wrapper example",
"fieldName": "for"
}
],
"superclass": {
Expand Down
2 changes: 2 additions & 0 deletions demo/lit-app/solid-js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ type RadioButtonProps = {
disabled?: RadioButton["disabled"];
/** A lookup type for example */
target?: RadioButton["target"];
/** A mapped attribute for react wrapper example */
for?: RadioButton["for"];
};

export type CustomElements = {
Expand Down
3 changes: 3 additions & 0 deletions demo/lit-app/src/radio-button/RadioButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export class RadioButton extends LitElement {
/** A lookup type for example */
@property() target?: Target;

/** A mapped attribute for react wrapper example */
@property() for?: string;

render() {
return html`
<h2>Hello world!</h2>
Expand Down
5 changes: 5 additions & 0 deletions demo/lit-app/vscode.html-custom-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
{ "name": "_parent" },
{ "name": "_top" }
]
},
{
"name": "for",
"description": "A mapped attribute for react wrapper example",
"values": []
}
],
"references": []
Expand Down
10 changes: 10 additions & 0 deletions demo/lit-app/web-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
"value": {
"type": "'_blank' | '_self' | '_parent' | '_top' | undefined"
}
},
{
"name": "for",
"description": "A mapped attribute for react wrapper example",
"value": { "type": "string | undefined" }
}
],
"slots": [
Expand Down Expand Up @@ -224,6 +229,11 @@
"value": {
"type": "'_blank' | '_self' | '_parent' | '_top' | undefined"
}
},
{
"name": "for",
"description": "A mapped attribute for react wrapper example",
"value": { "type": "string | undefined" }
}
],
"events": []
Expand Down
5 changes: 5 additions & 0 deletions packages/react-wrappers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 1.1.0

- Changed attribute binding to add attributes before rendering
- Fixed attribute mapping error

## 1.0.6

- Fixed missing details in component description
Expand Down
2 changes: 1 addition & 1 deletion packages/react-wrappers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "custom-element-react-wrappers",
"version": "1.0.6",
"version": "1.1.0",
"description": "A tool for generating react-compatible wrappers for custom elements",
"main": "index.js",
"module": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-wrappers/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function saveReactUtils(outdir: string) {
import { useEffect, useLayoutEffect } from "react";
export function useAttribute(targetElement, attrName, value) {
useEffect(() => {
useLayoutEffect(() => {
if (value !== undefined && attrName !== 'style' && targetElement.current?.getAttribute(attrName) !== String(value)) {
targetElement.current?.setAttribute(attrName, String(value));
}
Expand Down
1 change: 1 addition & 0 deletions packages/react-wrappers/src/wrapper-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function updateConfig(options: Options) {
config.exclude = options.exclude || [];
config.typesSrc = options.typesSrc || "types";
config.modulePath = options.modulePath;
config.attributeMapping = options.attributeMapping || {};
globalEvents = [...baseEvents, ...(options.globalEvents || [])];
}

Expand Down

0 comments on commit 76a283b

Please sign in to comment.