Skip to content

kruemelo/preacts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

preacts

Run preact standalone in the browser.

  • no transpiler necessary
  • no build chain
  • just import one tiny ~12kB file

Supports

  • Writing html and css Template literals
  • Custom Components. Generate and register a custom element from a preact component.
  • css rendering using CSSStyleSheet

Based on

Readings

Install

npm:

npm i --save-dev @kruemelo/preacts
cp node-modules/@kruemelo/preacts/dist/preacts.js <your/web/path/here/>preacts.js

Replace <your/web/path/here/> with your desired target location.

Use

index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script type="importmap">
      {
        "imports": {
          "preacts": "<your/web/path/here/>/preacts.js"
        }
      }
    </script>    
  </head>
  <script type="module">
    import { html, render, signal, renderToString } from "preacts";
    import { Greeting } from "./Greeting.js";
  
    const count = signal(0);

    function App() {
      return html`
        <div>
          <${Greeting} count=${count.value} />
          <button onClick=${() => (count.value += 1)}>
            Increment with signal
          </button>
          <p>Counter: ${count}</p>
          <p>Greeting rendered to string: 
          <pre>${renderToString(html`<${Greeting} count=${count.value} />`)}</pre>
          </p>
        </div>
      `;
    }

    render(html`<${App} />`, document.body);
  </script>
</html>

You need to adjust importmap: Replace <your/web/path/here/> with the path the preacts.js file is accessible for the browser.

Greeting.js:

import { html, css } from "preacts";

export const Greeting = ({ name = 'world', count }) => {
  return html`
    <h1 class="Greeting">
      Hello, ${count === 0 ? name : "again"}!
    </h1>
  `;
};

// css
const backgroundColor = "lightgreen";
const style = css`
  .Greeting {
    background-color: ${backgroundColor};
  }
`;

css.render(style);

Build lib

npm run build

Run

# prerequisites
npm install http-server -g
npm i
# startup the server open test in browser
http-server -o /test

About

preact standalone

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published