Hi, I'm Atomico js and I bring you the React syntax for webcomponents, I think you and I get along very well 😊.
import { Button } from "@formas/button/react";
function App(){
return <>
<h1>React App!</h1>
<Button onClick={()=>console.log("Click!")}>
Submit
</Button>
</>
}const props allows us to create the properties of our webcomponent, these are like React's propTypes, but with a big difference they are associated with the instance and can be read and modified by referencing the node, example document.querySelector("my-counter").count = 10;import { useState } from "react";
import ReactDOM from 'react-dom'
function Counter({initialCount}) {
const [count, setCount] = useState(initialCount);
return (
<>
Count: {count}
<button onClick={() => setCount(initialCount)}>Reset</button>
<button onClick={() => setCount(prevCount => prevCount - 1
<button onClick={() => setCount(prevCount => prevCount + 1
</>
);
}
render(
<Counter initialCount={1}/>,
document.querySelector("#counter")
);import { c, useProp } from "atomico";
const props = { count: { type: Number, value: 0
const Counter = c(
() => {
const [count, setCount] = useProp("count");
return (
<host>
Count: {count}
<button onClick={() => setCount((prevCount) => prevCount - 1
<button onClick={() => setCount((prevCount) => prevCount + 1
</host>
);
},
{ props }
);
customElements.define("my-counter", Counter);const Button = styled.a`
/* This renders the buttons above... Edit me! */
display: inline-block;
border-radius: 3px;
padding: 0.5rem 0;
margin: 0.5rem 1rem;
width: 11rem;
background: transparent;
color: white;
border: 2px solid white;
/* The GitHub button is a primary button
* edit this to target it specifically! */
${props => props.primary && css`
background: white;
color: black;
`}
`
render(
<div>
<Button
href="https://github.com/styled-components/styled-components"
target="_blank"
rel="noopener"
primary
>
GitHub
</Button>
<Button as={Link} href="/docs">
Documentation
</Button>
</div>
)import { c, css } from "atomico";
const props = { primary: { type: Boolean, relfect: true } };
const styles = css`
:host {
display: inline-block;
border-radius: 3px;
padding: 0.5rem 0;
margin: 0.5rem 1rem;
width: 11rem;
background: transparent;
color: white;
border: 2px solid white;
}
:host([primary]) {
background: white;
color: black;
}
`;
export const Button = c(
() => (
<host shadowDom>
<slot />
</host>
),
{ props, styles }
);
customElements.define("my-button", Button);
function Child({children}){
return <span>children</span>
}
function Main(){
return <>
<Child>text 1...</Child>
<Child>text 2...</Child>
</>
}import { c } from "atomico";
export const MyComponent = c(() => <host>...</host>); // Constructor
customElements.define("my-component", MyComponent);import { c } from "atomico";
import { MyComponent } from "./my-component";
export const MyApp = c(() => (
<host>
<MyComponent />
</host>
));
customElements.define("my-app", MyApp);function MyIcon({ size }) {
return (
<svg height={size} width={size}>
<circle r="45" cx="50" cy="50" fill="red" />
</svg>
);
}
const MyComponent = c(() => (
<host>
Small <MyIcon size={"1rem"} />
Large <MyIcon size={"2rem"} />
</host>
));