This is a demonstration of using Preact without any build tooling. The library is linked from the esm.sh CDN, however a standalone JS file exporting HTM + Preact + Hooks can also be downloaded here.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SETUP: install documentation.js: | |
npm i -g documentation browser-pipe | |
# SETUP: install gist cli (github.com/defunkt/gist) | |
brew install gist | |
# generate docs, upload to gist.github.com, then pipe the Gist URL to Documentation Viewer in your browser: | |
documentation src | gist -p -f docs.json | awk '{print "https://documentation-viewer.firebaseapp.com/#"$1}' | browser-pipe |
This is a standalone Preact 10+ implementation of the deprecated replaceNode
parameter from Preact 10.
It provides a way to render or hydrate a Preact tree using a subset of the children within the parent element passed to render():
<body>
<div id="root"> ⬅ we pass this to render() as the parent DOM element...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
env: { | |
browser: true | |
}, | |
extends: [ | |
'plugin:react/recommended', | |
'plugin:@typescript-eslint/recommended', | |
'prettier/@typescript-eslint', | |
'plugin:prettier/recommended' | |
], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2018 Google Inc. All Rights Reserved. | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { signal, effect } from "@preact/signals-core"; | |
interface CustomStorage { | |
getItem(key: string): void; | |
setItem(key: string, value: string | null): void; | |
} | |
/** | |
* A version of signal() that persists and recalls its value in localStorage. | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const { html, Component } = require('htm/preact'); | |
const renderToString = require('preact-render-to-string'); | |
class App extends Component { | |
render(props) { | |
return html` | |
<div class="app"> | |
<h1>This is an app</h1> | |
<p>Current server time: ${new Date + ''}</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const htm = require('htm'); | |
const vhtml = require('vhtml'); | |
// create an html`` tag function for vhtml: | |
const html = htm.bind(vhtml); | |
const App = (props) => html` | |
<div class="app"> | |
<h1>This is an app</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* cloneNode(true), but also clones shadow roots. | |
* @param {Element} | |
* @param {ShadowRoot[]} [shadowRoots] Any closed shadow roots passed here will be included. | |
*/ | |
function cloneWithShadowRoots(node, shadowRoots) { | |
function walk(node, clone) { | |
let shadow = node.shadowRoot || shadowRoots.find(r => r.host === node); | |
if (shadow) { | |
clone.attachShadow({ mode: shadow.mode }).append(...[].map.call(shadow.childNodes, c => c.cloneNode(true))); |
Loading modern code for modern browsers while still supporting older browsers should be possible via module/nomodule:
<script type="module" src="/modern.js"></script>
<script nomodule src="/legacy.js"></script>
... however this results in over-fetching of scripts in Edge and Safari.
NewerOlder