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

tw-tailwind

v1.0.21

Published

A lighter version of twin.macro that allows you to create tailwind react components like styled-components

Downloads

126

Readme

Tw-Tailwind

A lighter version of twin.macro that allows you to create tailwind react components like styled-components

NPM version

Install

Using npm

npm i tw-tailwind

Using yarn

yarn add tw-tailwind

⚠️ Tw-Tailwind requires TailwindCSS to be installed and configured on your project too. Install TailwindCSS

Usage

Import

import tw from 'tw-tailwind'

Features

const sharedClasses = tw`border-red-500`
const Component = tw.div`flex items-center justify-center`

const Component = tw(Button)`flex items-center justify-center`
interface Props {
  $hasBorder: boolean
}
const Component = tw.div<Props>`
  bg-red-500 
  ${({ $hasBorder }) => $hasBorder && 'border-2 border-blue-500'}`

const Component = tw(Button)<Props>`
  bg-red-500 
  ${({ $hasBorder }) => $hasBorder && 'border-2 border-blue-500'}`
const Component = tw.div(() => ['bg-red-500'])

const Component = tw(Button)(() => ['bg-red-500'])
interface Props {
  $hasBorder: boolean
}
const Component = tw.div<Props>(({ $hasBorder }) => [
  'bg-red-500',
  $hasBorder && 'border-2 border-blue-500',
])

const Component = tw(Button)<Props>`
  bg-red-500 
  ${({ $hasBorder }) => $hasBorder && 'border-2 border-blue-500'}`

Example

Button

import tw from 'tw-tailwind'

const colors = {
  white: tw`bg-white hover:bg-gray-100 text-black`,
  gray: tw`bg-gray-100 hover:bg-gray-200 text-black`,
  emerald: tw`bg-emerald-600 hover:bg-emerald-700 text-white`,
}
const loadingColors: typeof colors = {
  white: tw`text-white`,
  gray: tw`text-gray-100`,
  emerald: tw`text-emerald-600`,
}

type ButtonProps = Omit<JSX.IntrinsicElements['button'], 'ref'> & {
  color?: keyof typeof colors
  isLoading?: boolean
}

export const Button = ({ children, className, type = 'button', color = 'emerald', isLoading, ...props }: ButtonProps) => (
  <ButtonStyled type={type} {...props} $color={color} $isLoading={isLoading} className={className}>
    {children}
  </ButtonStyled>
)

const ButtonStyled = tw.button<{ $color: keyof typeof colors; $isLoading?: boolean }>(({ $isLoading, $color }) => [
  `disabled:opacity-30 disabled:pointer-events-none text-white font-medium rounded-md px-4 h-10 flex items-center relative select-none`,
  colors[$color],
  $isLoading && ['pointer-events-none', loadingColors[$color]],
])

Input

import { forwardRef, ReactNode } from 'react'
import tw from 'tw-tailwind'
import { Label } from './label'
import clsx from 'clsx'

const colors = { white: tw`bg-white `, gray: tw`bg-gray-200` }

type InputProps = Omit<JSX.IntrinsicElements['input'], 'ref'> & {
  label?: string
  icon?: ReactNode
  className?: string
  color?: keyof typeof colors
}
export const Input = forwardRef<HTMLInputElement, InputProps>(
  ({ label, className, icon, color = 'white', ...props }, ref) => (
    <Container className={className}>
      {label && <Label>{label}</Label>}
      <Wrapper>
        <InputStyled className={clsx([colors[color], !!icon && `pl-10 `])} {...props} ref={ref} />
        <IconWrapper>{icon}</IconWrapper>
      </Wrapper>
    </Container>
  ),
)

Input.displayName = 'Input'

const InputStyled = tw.input`bg-white rounded-md px-3 shadow-sm h-10 flex border border-gray-300 items-center w-full pb-px`
const Container = tw.div`text-sm w-full`
const Wrapper = tw.div`relative`
const IconWrapper = tw.div`absolute text-xl h-10 px-3 flex justify-center items-center top-0 left-0`

VS Code IntelliSense

Install "Tailwind CSS IntelliSense" VSCode - extension for autocomplete

https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss

  // vs code setting.json

  "scss.validate": false,
  "editor.quickSuggestions": {
    "strings": true
  },
  "editor.autoClosingQuotes": "always",
  "tailwindCSS.experimental.classRegex": [
    "tw`([^`]*)", // tw`...`
    "tw\\.[^`]+`([^`]*)", // tw.xxx`...`
    "tw\\(.*?\\).*?`([^`]*)", // tw(Component)`...`
    ["tw\\.[^\\)]+\\(.*=>([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"], // tw.xxx(()=> ['...'])
    ["tw\\(.*?\\)\\(.*=>([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"], // tw(Component)(()=> ['...'])
    ["tw\\(.*?\\).*?\\(.*=>([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"], // tw(Component)<...>(()=> ['...'])
    ["clsx\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"] // clsx( ['...'])
  ],

  "tailwindCSS.includeLanguages": {
    "typescript": "javascript",
    "typescriptreact": "javascript"
  },