Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
/ stitchtail Public archive

Generate classes depending on passed props using a base, variants, and compounds.

License

Notifications You must be signed in to change notification settings

bryiac/stitchtail

Repository files navigation

⚠️ ARCHIVED ⚠️

~ stitchtail ~

Generate classes depending on passed props using a base, variants, and compounds.

🧐 installation

# npm
npm install stitchtail

#yarn
yarn add stitchtail
  • Using tailwind and its intellisense? Try out the intellisense with stitchtails by simply adding this to your visual studio code settings.json.
"tailwindCSS.experimental.classRegex": [
	["stitchtail(?:<.+>)?\\({([\\s\\S]*)}\\)", "(?<!\\[{[^}]*?)[\"'`]([^\"'`]*)[\"'`]"]
]

😎 examples

 Typescript (https://stackblitz.com/edit/stitchtail-typescript)

Code:

src/components/Component.tsx
import React from "react";
import stitchtail from "stitchtail";

interface Props {
  colors: "red" | "green" | "blue",
  disabled?: boolean,
  children?: React.ReactNode
};

const classes = stitchtail<Props>({
  base: "rounded-sm p-4 text-white text-center",

  variants: {
    colors: {
      red: "bg-red-500",
      green: "bg-green-500",
      blue: "bg-blue-500"
    },
    disabled: "cursor-not-allowed"
  },

  compounds: [
    [{ colors: "red", disabled: true }, "bg-pink-500"]
  ]
});

const Component = (props: Props) => {
  return (
    <div className={classes(props)}>
      {props.children}
    </div>
  );
};

export default Component;
src/index.tsx
import React from "react";
import ReactDOM from "react-dom";

// Components
import Component from "./components/Component";

ReactDOM.render(
  <Component colors="red" disabled>
    Content
  </Component>,
  document.getElementById("root")
);

Result:

result

 Javascript (https://stackblitz.com/edit/stitchtail-javascript)

Code:

src/components/Component.js
import React from "react";
import stitchtail from "stitchtail";

const classes = stitchtail({
  base: "rounded-sm p-4 text-white text-center",

  variants: {
    colors: {
      red: "bg-red-500",
      green: "bg-green-500",
      blue: "bg-blue-500"
    },
    disabled: "cursor-not-allowed"
  },

  compounds: [
    [{ colors: "blue", disabled: true }, "bg-purple-500"]
  ]
});

const Component = (props) => {
  const { children, ...rest } = props;

  return (
    <div className={classes(rest)}>
      {props.children}
    </div>
  );
};

export default Component;
src/index.js
import React from "react";
import ReactDOM from "react-dom";

// Components
import Component from "./components/Component";

ReactDOM.render(
  <Component colors="blue" disabled>
    Content
  </Component>,
  document.getElementById("root")
);

Result:

result

  • It was intended to be used with tailwind, but you could also use your own classes or classes generated from some other library.

🥳 thanks

  • stitches.dev for inspiration on syntax and the whole concept in general.

  • typescript community for help with generics and other issues encountered whilst making types.

🤔 contributing

🥸 license

  • Licensed under MIT, see the license file for more information.

About

Generate classes depending on passed props using a base, variants, and compounds.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published