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

midashi

v0.3.2

Published

React component for headings

Downloads

6

Readme

Midashi

Introduction

This is a library for React that concisely manages heading levels. Dependencies are React only, and 100% developed in TypeScript.

Installation

npm install midashi
yarn add midashi
pnpm add midashi
bun install midashi

Concept

The h1-h6 tags used to render the heading level are simple, but they can easily create impossible situations in the document structure. For example

export default function App() {
  return (
    <main> {/* Missing h1 */}
      <h2>Heading 2</h2>
      <h3>Heading 3</h3> {/* Same section in the same heading level */}
    </main> 
  )
}

In the actual product, the code will be split and possibly commonized. The larger the application, the more difficult it becomes to detect. This library provides a thin wrapper around the h tag and the tags that represent the document structure so that the tag itself can determine which heading level it should provide.

import { Main, Section, H } from 'midashi';

const App = () => (
  <>
   <H>Heading 1</H>
    <Main>
      <H>Heading 2</H>
      <Section>
         <H>Heading 3</H>
         <Section>
           <H>Heading 4</H>
           <Section>
             <H>Heading 5</H>
             <Section>
               <H>Heading 6</H>
             </Section>
         </Section>
      </Section>
    </Main>
  </>
);

Can be described in the same way as a normal h1-h6, section, and main DOM component

This is rendered as follows:

<h1>Heading 1</h1>
<main>
  <h2>Heading 2</h2>
  <section>
    <h3>Heading 3</h3>
    <section>
      <h4>Heading 4</h4>
      <section>
        <h5>Heading 5</h5>
        <section>
          <h6>Heading 6</h6>
        </section>
      </section>
    </section>
  </section>
</main>

API

<Main />, <Section />, <Header />, <Footer />, <Article />, <Aside />, <Nav />

These are components that draw the main, section, header, footer, article, aside, and nav elements. They are thin wrappers for their respective DOM components and can be used exactly like them. It acts as a Context Provider and can lower the heading level to get its children.

import { Main, Section, Header, Footer, Article, Aside, Nav, H } from 'midashi';

const Component = () => (
  <>
    <H>Heading 1</H>
    <Main>
      <H>Heading 2</H>
      <Section>
        <H>Heading 3</H>
        <Header>
          <H>Heading 4</H>
        </Header>
        <Footer>
          <H>Heading 4</H>
        </Footer>
        <Article>
          <H>Heading 4</H>
        </Article>
        <Aside>
          <H>Heading 4</H>
        </Aside>
        <Nav>
          <H>Heading 4</H>
        </Nav>
      </Section>
    </Main>
  </>
);

<NextHeadingProvider />

This is context provider that increments the heading level used inside DOM component wrappers. Works like them, but does not draw anything.

import { NextHeadingProvider } from 'midashi';
import { Container } from '@chakra-ui/react';

export const MyContainer:FC<{ children:ReactNode }> = ({ children }) => (
  <NextHeadingProvider>
    <Container>
      {children}
    </Container>
  </NextHeadingProvider>
);

useCurrentLevel, useNextLevel

Between h1 and h6 to get how many heading levels that hierarchy should draw. The value is a union type between h1 and h6, which is useful when creating your own components, such as in a switch statement. Use in combination with libraries as follows:

import { useCurrentLevel } from 'midashi';
import { Heading } from '@chakra-ui/react';

const MyHeading = () => {
  const as = useCurrentLevel();
  return <Heading as={as}>Heading</Heading>;
};

You can also create your own components with a pre-designed look and feel as follows:

import { useCurrentLevel} from 'midashi';

const MyHeading = () => {
  const level = useCurrentLevel();
  switch (level) {
    case 'h1':
      return <h1 className="site-title">Heading</h1>;
    case 'h2':
      return <h2 className="page-title">Heading</h2>;
    case 'h3':
      return <h3 className="section-title">Heading</h3>;
    case 'h4':
      return <h4 className="sub-section-title">Heading</h4>;
    case 'h5':
      return <h5 className="sub-sub-section-title">Heading</h5>;
    case 'h6':
      return <h6 className="sub-sub-sub-section-title">Heading</h6>;
  }
};

License

MIT