Code Monkey home page Code Monkey logo

proto-stencil-tailwind's Introduction

proto-stencil-tailwind

This package is used in order to integrate with the 2.x release of tailwindcss. It provides simple functionality for supporting a utility-first workflow within the Shadow DOM.

NOTE: this plugin specificially adds support for inline utilities... (idiomatic Tailwind)

Installation

First, yarn install within the project:

yarn add --dev proto-stencil-tailwind
yarn add --dev postcss 
yarn add --dev rollup
yarn add --dev tailwindcss

Next, within the project's stencil.config.js file:

  1. import the plugin
  2. update the plugins config
  3. update the reloadStrategy

stencil.config.ts

import { Config } from '@stencil/core'
import tailwind from 'proto-stencil-tailwind'

export const config: Config = {
  plugins: [
    tailwind()
  ],
  devServer: {
    reloadStrategy: 'pageReload'
  }
}

Create your Tailwind config file (optional)

While Tailwind provides a sensible default configuration, it is often desirable to further customize your theme. This default configuration can be used as a starting point for such customizations. To customize your Tailwind installation, you will first need to generate a config file for your project using the included Tailwind CLI utility when you install the proto-stencil-tailwind npm package.

npx tailwindcss init

This will generate a tailwind.config.js file at the root of your project.

Usage

Inline utilities

Utility classes can be used directly within JSX; they will be included in the component's shadow tree.

class MyComponent {
  render() {
    return (
      <div class="p-4 bg-red">
        <p class="text-sm text-white">This is JSX!</p>
      </div>
    );
  }
}

Options

The following plugin options may be configured:

stencil.config.ts

import { Config } from '@stencil/core'
import tailwindcss from 'tailwindcss'

export const config: Config = {
  plugins: [
    tailwind({
      tailwind: tailwindcss('tailwind.config.js'),
      inputFile: './src/styles/app.css',
      includeTailwindCss: false
    })
  ]
}
  • tailwind: (optional) your own configuration file and version of TailwindCSS to be used.
  • inputFile: (optional) a stylesheet filepath to be used in place of the default.
  • includeTailwindCss: (optional) include global tailwind.css in the bundle (default: true)

NOTE: adding an inputFile will give you the ability to support the creation of tailwind components...

Using the inputFile option

1. stencil.config.ts
import { Config } from '@stencil/core'
import tailwind from 'proto-stencil-tailwind'

export const config: Config = {
  plugins: [
    tailwind({
      inputFile: './src/styles/app.css'
    })
  ],
  devServer: {
    reloadStrategy: 'pageReload'
  }
}
2. src/styles/app.css
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer components {
  .btn-blue {
    @apply py-2 px-4 bg-blue-500 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400 focus:ring-opacity-75;
  }
}

This simple combination of changes creates a tailwind component class named btn-blue which you could use in your app instead of typing:

"py-2 px-4 bg-blue-500 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400 focus:ring-opacity-75"

You can read more about this approach here:

tw utility

I found a handy utility that works well with this plugin:

const tw = (...classes: (false | null | undefined | string)[]): string => {
  return classes.filter(Boolean).join(' ');
};

export { tw };
export default tw;

This tw utility was developed for React, but it works just as well for Stencil and this plugin.

For example:

    <Logo
      className={tw(
        'relative',
        'w-full h-auto',
        'mt-2 mb-4',
        'text-blue-600 fill-current'
      )}
    />

You can use this to breakup the long class strings and it also gives you a way to provide conditional styling using ternary expressions:

    <div
      class={tw(
        'flex align-middle',
        'rounded-lg p-4 mb-1',
        'border border-solid',
        isExotic(group)
          ? 'bg-gray-300 border-gray-600'
          : 'bg-green-200 border-green-600',
      )}
    >

The plugin will correctly find all of the tailwind classes when using this approach and make them available in the associated component root. You can find the article about this trick in the references below.

VSCode

I found this plugin for VSCode useful when working with Tailwind:

plug

It's not perfect, but for the traditonal Tailwind workflow, it works pretty well (YMMV).

Credits

Thanks goes to Jack Rowlingson and all of the others who contributed to the Tailwind 1.x version of this plugin!!

References

proto-stencil-tailwind's People

Contributors

dependabot[bot] avatar dohxis avatar eswat2 avatar joewoodhouse avatar jrowlingson avatar kstinson14 avatar robbiethewagner avatar ssfinney avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

proto-stencil-tailwind's Issues

refactor to support named exports

it would be nice to support named exports instead of only default. For example:

export { tailwind, tw }
export default tailwind

where:

  • tailwind is the current default export (aka. the plugin)
  • tw is a handy utility that works well with this plugin

issues:

  • getting the typings right in the index.d.js

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.