npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

adonis-preact

v1.0.0

Published

Preact provider for AdonisJS 5

Downloads

2

Readme

Adonis Preact

NPM version build status npm download

Preact provider for AdonisJS.

Prerequisites

This provider requires AdonisJS v5 preview and won't work with AdonisJS v4.

Installation

npm i adonis-preact preact
node ace invoke adonis-preact

You'll also need to add "jsx": "react" to the compilerOptions of your TypeScript configuration.

Usage

Writing a layout component

adonis-preact doesn't create any HTML for you, so you will probably need to write a layout component that contains the base structure for your page:

// app/Components/layouts/Base.tsx

/** @jsx h */
import { h } from "preact";

export default function Base(props) {
  return (
    <html lang="en">
      <head>
        <meta charSet="UTF-8" />
        <title>My Adonis website</title>
      </head>
      <body>{props.children}</body>
    </html>
  );
}

Writing a page component

Now that you have a layout component, you can use it in any page component (the component that will be rendered from your controllers):

// app/Components/pages/Index.tsx

/** @jsx h */
import { h } from "preact";
import Base from '../layouts/Base';

export default function Index() {
  return (
    <Base>
      <div className="max-w-screen-xl mx-auto text-center py-16 px-8">
        <h2 className="font-extrabold tracking-tight text-gray-900 text-4xl leading-10">
          Hello from Adonis Preact!
        </h2>
      </div>
    </Base>
  );
}

Rendering a component

In a route handler

// start/routes.ts

import Route from '@ioc:Adonis/Core/Route';

import Index from 'App/Components/pages/Index';

Route.get('/', async ({ preact }) => preact.render(Index));

In a controller

// app/Controllers/Http/MyController.ts

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';

import Index from 'App/Components/pages/Index';

export default class MyController {
  index({ preact }: HttpContextContract) {
    return preact.render(Index);
  }
}

Passing props to the rendered component

If the component you render expects props, they must be passed as a second argument to the render method:

// app/Components/pages/WithProps.tsx

/** @jsx h */
import { h } from "preact";
import Base from '../layouts/Base';

export default function WithProps(props: { name: string }) {
  return (
    <Base>
      <div className="max-w-screen-xl mx-auto text-center py-16 px-8">
        <h2 className="font-extrabold tracking-tight text-gray-900 text-4xl leading-10">
          Hello {props.name}!
        </h2>
      </div>
    </Base>
  );
}
// start/routes.ts

import Route from '@ioc:Adonis/Core/Route';

import Index from 'App/Components/pages/Index';

Route.get('/with-props', async ({ preact }) =>
  preact.render(WithProps, { name: 'my friend' }),
);

Accessing the Adonis context from a component

Adonis Preact provides a hook to access the Adonis context and some helpers from anywhere inside the component tree.

// app/Components/pages/WithContext.tsx

import { useAdonisContext } from '@ioc:Preact';
/** @jsx h */
import { h } from "preact";
import Base from '../layouts/Base';

export default function WithContext(props: { name: string }) {
  const { request } = useAdonisContext();

  return (
    <Base>
      <div className="max-w-screen-xl mx-auto text-center py-16 px-8">
        <h2 className="font-extrabold tracking-tight text-gray-900 text-4xl leading-10">
          Hello from {request.url()}!
        </h2>
      </div>
    </Base>
  );
}

// start/routes.ts

import Route from '@ioc:Adonis/Core/Route';

import WithContext from 'App/Components/pages/WithContext';

Route.get('*', async ({ preact }) => preact.render(WithContext));

License

  • Based on adonis-react package

MIT