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

@strategies/henshu

v1.1.6

Published

React library for editable interfaces

Downloads

665

Readme

Henshu

Henshu is Sasaki's solution for making it easy for developers to provide seamless, editable interfaces to content owners.

We wanted to build a CMS layer that was low overhead and allowed teams to toggle into an edit mode directly on our websites where they can edit the content in-place. Many other CMS solutions require a lot of meta programming, pulling the content away from the place where you are developing and your editors are editing.

Documentation

Install

yarn add @strategies/henshu

then

import { Henshu, henshu, useHenshu, HenshuContent } from '@strategies/henshu';

Henshu In Three Layers

The Elements

Converting to Henshu

The bulk of Henshu is at the component level, where you write your HTML how you normally would but namespaced to Henshu. The elements that are Henshu-enabled are outlined in more detail in the API section below.

const Example = () => (
    <>
        <h1>This is a header</h1>
        <p>Some paragraph text</p>
    </>
);

becomes

const Example = () => (
    <>
        <henshu.h1 get={() => "This is a header"} />
        <henshu.p get={() => "Some paragraph text"} />
    </>
);

Getting new values from Henshu

As you can see from above, we namespaced the elements and then provided them with a get function prop. To retrieve the edited content from inside of the Henshu element, we use a set function prop. Values returned are always strings.

const Example = () => {
    const [header, setHeader] = useState("This is a header");
    const [paragraph, setParagraph] = useState("Some paragraph text");

    return (
        <>
            <henshu.h1 get={() => header} set={value => setHeader(value)} />
            <henshu.p get={() => paragraph} set={value => setParagraph(value)} />
        </>
    );
};

The Provider

The next layer is our provider, which delegates state to the Henshu elements and ties them back together into a shared state.

const App = () => {
    const [isEditing, setIsEditing] = useState(false);
    const [content, setContent] = useState<HenshuContent>({});

    return (
        <Henshu content={content} onChange={tree => setContent(tree)} editing={isEditing}>
            <Example />

            <button onClick={() => setIsEditing(!isEditing)}>
                Toggle Edit Mode
            </button>
        </Henshu>
    );
}

The Context

The content prop that we passed to the provider is accessed through our final piece of Henshu, which is the useHenshu() hook. This hook provides two things: the current state of the editing prop and a bindTo(). The latter function takes one argument and returns an object with .get() and .set() bound to a node on the object passed through the content prop. The final form of our full example now looks like:

const Example = () => {
    const { bindTo } = useHenshu();

    return (
        <>
            <henshu.h1 {...bindTo('header')} />
            <henshu.p {...bindTo('paragraph')} />
        </>
    );
};

const App = () => {
    const [isEditing, setIsEditing] = useState(false);
    const [content, setContent] = useState<HenshuContent>({
        "header": "This is a header",
        "paragraph": "Some paragraph text"
    });

    return (
        <Henshu content={content} onChange={tree => setContent(tree)} editing={isEditing}>
            <Example />

            <button onClick={() => setIsEditing(!isEditing)}>
                Toggle Edit Mode
            </button>
        </Henshu>
    );
};

API

Henshu Elements

All Henshu elements are available from the henshu component and have get/set props, which means all Henshu elements can use the bindTo() helper.

Plain Text

The plain text elements allow editing of the elements text content.

Example
<henshu.p {...bindTo('app.landing.intro')} />

Plain text elements are: a, b, button, div, em, i, h1-6, label, li, p, span, strong

Rich Text

You can access a Quill component by using

<henshu.richtext />

Images

Images can be uploaded by using

<henshu.img />

The value provided to the set function prop is a base64 data uri of the image uploaded.

Lists

Henshu provides an component list editor via an each component. This special component passes a nested bindTo() function that iterates through the lists items.

<henshu.each {...bindTo('app.people'}>
	{(bindToItem, i) => (
		<>
			<henshu.h2 {...bindToItem('name')} />
			<henshu.p {...bindToItem('bio')} />
		</>
	)}
</henshu.each>

The each component takes an optional prop, max, which specifies the max length of the list if you want to restrict the content editor from creating too many list items.

Henshu Provider

The Henshu provider component manages what its descendant henshu elements have in their scope. Since the provider component uses React's context API, you can have multiple Henshu providers throughout your site if you want to segment your content nodes into a superstructure of your choice or maybe only allow certain privileges to a class of users.

const App = () => {
	const user  = useUser();
	const [generalContent, setGeneralContent] = useState<HenshuContent>({});
	const [priviledgedContent, setPriviledgedContent] = useState<HenshuContent>({});
	
	return <>
		<Henshu
			editing={user.isLoggedIn}
			content={generalContent}
			onChange={setGeneralContent}
		>
			<GeneralSection />
		</Henshu>
		<Henshu
			editing={user.isLoggedIn && user.isPriviledged}
			content={priviledgedContent}
			onChange={setPriviledgedContent}
		>
			<PriviledgedSection />
		</Henshu>
	</>;
};

Content

The content prop of the provider component should be an object since Henshu operates on a key/value mechanism. Your updated content will only stick if you update the object being passed to the content prop through the onChange prop.

Editing

All of the descendant Henshu elements become editable by setting the boolean prop editing.