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

@flemminghansen/design-block

v1.1.2

Published

Simplify design by controlling spacing declaratively using design blocks.

Downloads

594

Readme

What is a Design Block?

Imagine that you could make custom HTMLDivElement, but instead of a huge bundle of css-classes to define spacing, you use attributes. But these attributes are locked to given values, limiting margins or padding to values like "zero", "xs", "sm", "md", "lg", "xl", "xxl". This will make your design pattern a lot more consistant, while being extremely easy to work with.

Installation

Installation is fairly simple using Node's package manager.

In your console, run:

npm install @flemminghansen/design-block

Usage

Since Design Block is a custom element, you'll need to define it first at the root of your app or module. If you are using SSR, you can define the custom component in an accompanied js file to your render.

import { DesignBlock, define } from "@flemminghansen/design-block";

define('element-name', DesignBlock);

"element-name" can be anything, as long as it starts with a lowercase letter and contains a hyphen. You can read more about defining names on custom elements here: valid-custom-element-name

Once a valid name is defined, you can then use the custom component as a normal HTMLElement as such:

<element-name p="sm" ml="xxl">Hello world!</element-name>

But! Currently, we are relying on the falback size values. To override the fallback values, we use css variables.

    :root {
        --xs: 1px; 
        --sm: 3px; 
        --md: 6px; 
        --lg: 12px; 
        --xl: 24px; 
        --xxl: 40px; 
    }

Default size values

  • zero: "var(--zero, 0)",
  • xs: "var(--xs, 2px)",
  • sm: "var(--sm, 4px)",
  • md: "var(--md, 8px)",
  • lg: "var(--lg, 12px)",
  • xl: "var(--xl, 20px)",
  • xxl: "var(--xxl, 32px)",

Using with React and Typescript

If you try to use a custom element with React and TypeScript, you'll need to define it in the JSX global scope

If you look in your tsconfig, you can typecally see where your typings are located. It may look something like this:

		"typeRoots": [
			"./node_modules/@types",
			"./typings/"
		],

In your typings folder, or whatever it is called, there is usually a file where you define your globals. In that file, you define the custom element as such:

import React from "react";
import type { IDesignBlock } from "@flemminghansen/design-block";

export {};

type ReactWebComponent<T> = T & React.HTMLAttributes<HTMLElement>;

declare global {
  namespace JSX {
    interface IntrinsicElements {
      "element-name": ReactWebComponent<IDesignBlock>;
    }
  }
}

You should now be able to use the custom-element without type errors.

Attributes

Design block is based on flex with column as direction to simulate a normal block, but with the benefit of being able to use columnGap and rowGap as properties.

direction

flexDirection

"column" | "row"

Defaults to "column".

transition

transition

string

Defaults to "var(--transition, all 300ms)".

p

padding

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

px

padding-left and padding-right

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

py

padding-top and padding-bottom

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

pt

padding-top

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

pr

padding-right

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

pb

padding-bottom

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

pl

padding-left

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

m

margin

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

mx

margin-left and margin-right

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

my

margin-top and margin-bottom

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

mt

margin-top

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

mr

margin-right

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

mb

margin-bottom

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

ml

margin-left

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

rowgap

rowGap

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

colgap

columnGap

"zero" | "xs" | "sm" | "md" | "lg" | "xl" | "xxl"

Defaults to undefined.

Customization

Because the DesignBlock class is an extended HTMLElement, you can use it to create multiple Design blocks by extending the DesignBlock class.

import { DesignBlock, define } from "@flemminghansen/design-block";

class MyContainer extends DesignBlock {
  constructor() {
    super();
    // use custom properties to set values, or simple values like "20px" or "#eee"
    this.style.background = "var(--container-background)";
    this.style.borderRadius = "var(--border-radius)";
    this.style.border = "var(--border)";
    this.style.rowGap = "var(--lg)";
    this.style.padding = "var(--xxl)";
  }
}

define('my-container', MyContainer);
define('design-block', DesignBlock);

Then you can use the elements as you like:

<body>
    <style>
      :root {
        --container-background: #eee;
        --border-radius: 3px;
        --border: 1px solid #aaa;
        --xs: 2px; 
        --sm: 4px; 
        --md: 8px; 
        --lg: 12px; 
        --xl: 20px; 
        --xxl: 32px; 
      }

      @media (max-width: 60em) {
        :root {
            --xs: 1px; 
            --sm: 2px; 
            --md: 4px; 
            --lg: 8px; 
            --xl: 12px; 
            --xxl: 20px; 
      }
    }
  </style>
  <my-container pt="zero">
    <design-block>Hello world!</design-block>
    <design-block direction="row" colgap="lg">
      <span>We are using design blocks</span>
      <span>With columns</span>
      <span>And rows!</span>
    </design-block>
  </my-container>
  {/* etc... */}
</body>