Comparing version 2.0.1-canary-2fbaf0e276032505bfe291ac1d05d3840fcf8615.0 to 2.1.0
@@ -33,4 +33,5 @@ "use strict"; | ||
__reExport(src_exports, require("./jsx/jsx.types.js"), module.exports); | ||
__reExport(src_exports, require("./jsx/prop-types/index.js"), module.exports); | ||
var import_context_map = require("./context-map/context-map.js"); | ||
var import_render_to_html = require("./html-parser/render-to-html.js"); | ||
var import_render_to_string_template_tag = require("./string-template-parser/render-to-string-template-tag.js"); | ||
var import_context_map = require("./context-map/context-map.js"); |
import "./utilities/array-flat-polyfill"; | ||
export * from "./express/index"; | ||
export * from "./jsx/jsx.types"; | ||
export * from "./jsx/prop-types/index"; | ||
export { defineContext } from "./context-map/context-map"; | ||
export { renderToHtml, renderToHtmlAsync } from "./html-parser/render-to-html"; | ||
export { renderToStringTemplateTag } from "./string-template-parser/render-to-string-template-tag"; | ||
export { defineContext } from "./context-map/context-map"; | ||
export type { ContextDefinition, ContextMap } from "./context-map/context-map"; | ||
export type { AttributeBool, HTMLProps } from "./jsx/base-html-tag-props"; | ||
export type { StringTemplateParserOptions } from "./string-template-parser/render-to-string-template-tag"; | ||
export type { ContextDefinition, ContextMap } from "./context-map/context-map"; | ||
export type { HTMLProps, AttributeBool } from "./jsx/base-html-tag-props"; | ||
export type { Crossorigin } from "./jsx/prop-types/shared/crossorigin"; | ||
export type { RefererPolicy } from "./jsx/prop-types/shared/referer-policy"; | ||
export type { Target } from "./jsx/prop-types/shared/target"; |
@@ -20,3 +20,3 @@ { | ||
"name": "jsxte", | ||
"version": "2.0.1-canary-2fbaf0e276032505bfe291ac1d05d3840fcf8615.0", | ||
"version": "2.1.0", | ||
"main": "./dist/legacy/index.js", | ||
@@ -23,0 +23,0 @@ "types": "./dist/types/index.d.ts", |
@@ -115,7 +115,7 @@ # JSX Template Engine | ||
const myContext = defineContext<{ foo: string }>(); | ||
const myContext = defineContext<{ label: string }>(); | ||
const App: JSXTE.Component = (props, contextMap) => { | ||
// Set the context to a new value, all descendants of this component will have access to it | ||
contextMap.set(myContext, { foo: "Hello" }); | ||
contextMap.set(myContext, { label: "Hello" }); | ||
@@ -138,2 +138,53 @@ return <Foo />; | ||
### Provider/Consumer Pattern | ||
It is possible to incorporate a Provider/Consumer pattern with the ContextMap API. | ||
```tsx | ||
const makeContextWithProvider = <T,>() => { | ||
const ctx = defineContext<T>(); | ||
const Provider: JSXTE.Component<{ | ||
value: T; | ||
}> = (props, contextMap) => { | ||
contextMap.set(ctx, props.value); | ||
return <>{props.children}</>; | ||
}; | ||
const Consumer = ( | ||
props: { render: (value?: T) => JSX.Element }, | ||
contextMap: ContextMap | ||
) => { | ||
if (contextMap.has(ctx)) { | ||
const value = contextMap.get(ctx); | ||
return <>{props.render(value)}</>; | ||
} else { | ||
return <>{props.render()}</>; | ||
} | ||
}; | ||
return { | ||
context: ctx, | ||
Provider, | ||
Consumer, | ||
}; | ||
}; | ||
// Use it | ||
const MyContext = makeContextWithProvider<string>(); | ||
const App: JSXTE.Component = () => { | ||
return ( | ||
<MyContext.Provider value={"Hello World!"}> | ||
<div> | ||
<MyContext.Consumer | ||
render={(providedValue) => <h1>{providedValue ?? ""}</h1>} | ||
/> | ||
</div> | ||
</MyContext.Provider> | ||
); | ||
}; | ||
``` | ||
## Extending the typings | ||
@@ -140,0 +191,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
218852
291
5142
1
304
0