atomicojs / atomico

Atomico a micro-library for creating webcomponents using only functions, hooks and virtual-dom.
https://atomicojs.dev
MIT License
1.15k stars 43 forks source link

createContext and useContext for atomico@1.62.0 #85

Closed UpperCod closed 2 years ago

UpperCod commented 2 years ago

Describe the solution you'd like

Core level context api, example:

createContext

import {  createContext } from "atomico";

const MyContext = createContext({value: 0});

customElements.define("my-context", MyContext );

useContext

import {  useContext } from "atomico";
import { MyContext } from "./my-context";

function component(){
    const context = useContext(MyContext );
    return <host>{context.value}</host>
}

Additional context

This proposed API facilitates a connection between components and improves the typing of contexts at the TS level.

At a general level, an api based on a Consumer component is not exposed, since for this there is useContext at the internal level of Atomico

The return of createContext is an unregistered CustomElement