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

react-templated

v1.0.1

Published

Yet another implementation of slot components for React

Downloads

4

Readme

ci codecov downloads node npm MIT npm bundle size Conventional Commits

react-templated

Yet another implementation of slot components for React. It's inspired by WebComponents slots.

full documentation can be found on https://andres-kovalev.github.io/react-templated/

Description

react-templated provides several components to created templated components.

Installation

As any other npm package react-templated can be added to your project by following command:

npm i -S react-templated

It requires any version of react with new context API support as peer dependency, so it should be installed as well.

npm i -S react

Quick start

By default content for React component can be accessed by children prop:

function Parent() {
    return (
        <Child>
            <div>our div 1</div>
            <div>our div 2</div>
        </Child>
    );
}

...

function Child(props) {
    // here are our divs
    const { children } = props;

    ...
}

But some times we need to distribute children between different areas of our component. We might want to create a template. We can extract try to extract separate children and use those:

function Child({ children }) {
    const [ child1, child2 ] = children.props.children;

    return (
        <React.Fragment>
            <div className="header">
                {child1}
            </div>
            <div className="body">
                {child2}
            </div>
        </React.Fragment>
    );
}

Looks a bit ugly. Also, we need to provide some safe-checks/fallbacks to prevent issues when user provides less than 2 children and we need to find some way to be able to provide only 2nd/3rd/etc item.

import _ from 'lodash';

function Child({ children }) {
    const [ child1, child2 ] = _.get(children, 'props.children', []);

    ...
}

function Parent() {
    return (
        <React.Fragment />
        <div>body</div>
    );
}

Another way is to use different props for different areas:

const Page = ({ header, body, footer }) => (
    <div className="page">
        <div className="header">
            { header }
        </div>
        <div className="body">
            { body }
        </div>
        <div className="header">
            { footer }
        </div>
    </div>
)

Looks much better, but it's still not so convenient to use such components and provide default content:

const App = () => (
    <Page
        header={ <Header /> }
        body={ <Body /> }
        footer={ <Footer /> }
        />
);

// or

const App = () => {
    const header = <Header />;
    const body = <Body />;
    const footer = <Footer />;

    const props = { header, body, footer };

    return <Page { ...props } />;
}

One more option is to use slots from react-templated package. First we need to create our template:

import { Slot, withSlots, Value } from 'react-templated';

// it's not necessary to provide any props for template
const PageTemplate = () => (
    <div className="page">
        <div className="header">
            <Slot name="header">
                Slot content will be found by slot name
                We can provide default content for the slot here
            </Slot>
        </div>
        <div className="body">
            <Slot>
                We can skip name to mark slot as default
            </Slot>
        </div>
        <div className="header">
            <Slot name="footer" />
            {/* it's not necessary to provide default content */}
        </div>
    </div>
);

The next step is to enhance our component with slot power:

const Page = withSlots(PageTemplate);

Now we can provide any content for Page component and it will be distributed automatically:

const App = () => (
    <Page>
        <Value name="header">
            Header content
        </Value>
        <Value name="footer">
            Footer content
        </Value>
        Everything else will be considered as default content
        and will appear in default slot
    </Page>
);

That's it. For those, who don't like HoCs react-templated provides Templated wrapper:

import { Templated } from 'react-templated';

const Page = ({ children }) => (
    <Templated content={ children }>
        <PageTemplate />
    <Templated>
);

The code abowe is equivalent to withSlots() HoC.