• Stars
    star
    116
  • Rank 297,463 (Top 6 %)
  • Language
    TypeScript
  • Created almost 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate Elm code for Tailwind Utilities and Components that purges using Elm's dead code elimination!

Elm Tailwind Modules

Generate Elm code for Tailwind Utilities and Components which purges using Elm's dead code elimination!

Use Tailwind with Elm-Css!

Quick Start

If you want to try out how using elm-css with tailwind feels like without using npm, try out the package with prebuilt modules: elm-default-tailwind-modules

Install

$ npm i --save-dev elm-tailwind-modules tailwindcss postcss
$ npx elm-tailwind-modules --dir ./gen
Saved
 - gen/Tailwind/Utilities.elm
 - gen/Tailwind/Theme.elm
 - gen/Tailwind/Breakpoints.elm
$ elm install rtfeldman/elm-css
$ elm install matheus23/elm-tailwind-modules-base # used by the generated code internally

This will generate these files:

Use

Now you simply import these modules and use them in your elm code:

import Css
import Css.Global
import Html.Styled as Html
import Html.Styled.Attributes as Attr
import Tailwind.Breakpoints as Breakpoints
import Tailwind.Utilities as Tw
import Tailwind.Theme as Tw


main =
    Html.toUnstyled <|
        Html.div [ Attr.css [ Tw.bg_color Tw.gray_50 ] ]
            [ -- This will give us the standard tailwind style-reset as well as the fonts
              Css.Global.global Tw.globalStyles
            , Html.div
                [ Attr.css
                    [ Tw.mt_8
                    , Tw.flex

                    -- We use breakpoints like this
                    -- However, you need to order your breakpoints from high to low :/
                    , Breakpoints.lg [ Tw.mt_0, Tw.flex_shrink_0 ]
                    ]
                ]
                [ Html.div [ Attr.css [ Tw.inline_flex, Tw.rounded_md, Tw.shadow ] ]
                    [ Html.a
                        [ Attr.css
                            [ Tw.inline_flex
                            , Tw.items_center
                            , Tw.justify_center
                            , Tw.px_5
                            , Tw.py_3
                            , Tw.border
                            , Tw.border_color Tw.transparent
                            , Tw.text_base
                            , Tw.font_medium
                            , Tw.rounded_md
                            , Tw.text_color Tw.white
                            , Tw.bg_color Tw.indigo_600

                            -- We can use hover styles via elm-css :)
                            , Css.hover [ Tw.bg_color Tw.indigo_700 ]
                            ]
                        , Attr.href "#"
                        ]
                        [ Html.text "Get started" ]
                    ]
                ]
            ]

The result looks like this:

Screenshot

(For a bigger example, see test-example/src/Main.elm and related files.)

CLI


$ elm-tailwind-modules --help
Usage: elm-tailwind-modules [options]

Options:
  -V, --version             output the version number
  --dir <dir>               destination folder for generated elm modules, e.g. "src/" or "gen/". Add this folder to your elm.json
                            source-directories.
  --module-name <name>      module name prefix for generated elm modules, e.g. "Tailwind" or "Css.Gen"
  --tailwind-config <file>  your tailwind config file (default: null)
  -h, --help                display help for command

Nodejs API

This package is written in typescript, so you can use it from node via the same npm package, too.

The nodejs API allows you to do more stuff, for example, include additional postcss plugins like autoprefixer.

It boils down to this:

const elmTailwindModules = require("elm-tailwind-modules");
const tailwindConfig = require("./my-tailwind.js");
const autoprefixer = require("autoprefixer");

elmTailwindModules.run({
    directory: "./gen",
    moduleName: "Tailwind",
    postcssPlugins: [autoprefixer],
    tailwindConfig,
});

Full control

If you need even more control, you can integrate elm-tailwind-modules with your existing postcss pipeline by using it as a postcss plugin.

Below is an example of using elm-tailwind-modules asPostcssPlugin function to get following control:

  • Providing your own postcss file to consume

  • Adding the postcss-import plugin at the start of your postcss pipeline

  • Writing css that wasn't turned into elm definitions back as a css file

    (This can be useful, as the generated globalStyles definition has its limits, for example you can't use @font-face in elm-css.)

const elmTailwindModules = require("elm-tailwind-modules");
const tailwindConfig = require("./my-tailwind.js");
const autoprefixer = require("autoprefixer");
const postcssImport = require("postcss-import");
const postcss = require("postcss");
const tailwindcss = require("tailwindcss");
const fs = require("fs").promises;

const logFunction = message => console.log(message);

const moduleName = "Tailwind";

const elmTailwindModulesPlugin = elmTailwindModules.asPostcssPlugin({
    moduleName,
    tailwindConfig,
    generateDocumentation: true,
    logFunction,
    modulesGeneratedHook: async generated => elmTailwindModules.writeGeneratedFiles({
        directory: "gen",
        moduleName,
        logFunction,
        generated
    })
});

// This file has the postcss superpowers. So it includes things like
// * @tailwind base; @tailwind components; @tailwind utilities;
// * postcss-import's @import
// * tailwindcss' @apply
const inputCssFile = "./my-postcss.css";

// This file will contain basic css that every browser understands
const outputCssFile = "./build/stylesheet.css";

(async () => {
    const inputCss = await fs.readFile(inputCssFile, {
        encoding: "utf8"
    });

    const result = await postcss.default([
        // We can specify our own order of postcss plugins.
        postcssImport,
        tailwindcss(tailwindConfig),
        autoprefixer,
        elmTailwindModulesPlugin
    ]).process(inputCss, {
        from: inputCssFile,
        to: outputCssFile,
    });

    logFunction(`Saving remaining global css to ${outputCssFile}`);
    await fs.writeFile(outputCssFile, result.content);
})()

Node API Documentation

You can find the documentation at matheus23.github.io/elm-tailwind-modules.

Comparisons

  • monty5811/postcss-elm-tailwind:
    • You still need to include a .css file
    • You need to purge the .css file (which is a somewhat involved process, including having to run postcss twice)
    • The generated files contain a definition for all variants, which makes them much bigger (150+kLOC vs. 30+kLOC)
    • Has more configuration options
    • More mature and robust
  • Using classes via tailwind directly:
    • No type checking (typos might not get noticed)
    • global namespaces for classes

So mainly, the cool things about this package are:

  • You can use elm-css with tailwind. So:
    • No writing css by hand
    • No global css class namespaces
    • All the features of tailwindcss, its plugins and ecosystem
    • Simply import some elm files after generating them, and they're all you need

Acknowledgements

The idea for this is not original. It's a fork from justinrassier/postcss-elm-css-tailwind. Thanks!

More Repositories

1

RuinsOfRevenge

An RPG written in Java using LibGDX and Box2D. Maps loaded from Tiled and sprites / animations from .png / .xml /.json
Java
52
star
2

Utils

Noise classes, Matrix-to-array Mappers, and other useful java stuff wihtout dependencies to other libs.
Java
29
star
3

elm-default-tailwind-modules

The default tailwind classes in Elm as elm-css generated using elm-tailwind-modules
Elm
24
star
4

elmjs-inspect

Why is your elm.js file so big? Is Elm's dead code elimination on a particular function not working as you expected? What libraries cost your users bandwidth? Analyse your elm.js file size with this tool.
TypeScript
21
star
5

irreactive.com

My Blog about User Interface- and Functional Programming.
Elm
13
star
6

TinyWorld

Ludum Dare game entry from the 10th Anniversary Ludum Dare! Theme "Tiny World". Written in Java, rendering engine: Java2D.
Java
11
star
7

elm-markdown-transforms

For creating advanced elm-markdown renderers (e.g. with model access).
Elm
7
star
8

WorldOfCubeCpp

Terraria Clone, written in C++ with SDL.
C++
7
star
9

rust-set-reconciliation

An implementation of "Whatโ€™s the Difference? Efficient Set Reconciliation without Prior Context"
Rust
5
star
10

md-spellcheck-action

Spellcheck markdown files in your github action
TypeScript
4
star
11

dhall-blocks

More configureable dhall utility than Prelude/JSON/renderYAML and its internal Blocks structures.
Dhall
4
star
12

elm-figma-autoflex

Fetch figma frames via the Figma API and render them as responsive HTML
Elm
3
star
13

elm-drag-and-drop

Drag and drop with a similar api to html5-drag-and-drop, but implemented within elm.
Elm
3
star
14

elm-ide

Sematic Editor for Elm Code
Elm
3
star
15

WorldOfCube

A Game somehow similar to Minecraft in 2D, written in Java
Java
3
star
16

ScalaUtils

Utilities for scala, including Properties
Scala
2
star
17

ecc-acc-bls-test

Rust
2
star
18

nix

My NixOS configuration
Nix
2
star
19

UniverseEngine

A openGL 3D mid-level Engine, written in Java
Java
2
star
20

dynamic-visualisations

Haskell
1
star
21

flatmate

Elm
1
star
22

AnimationLoader

[Scala] Generic animation loading from .xml files. Supports filters and more complex stuff. Also includes a GUI application for viewing those animations and exporting those to .gif's.
Java
1
star
23

declarative-apis-widgets

Interactive code widgets used in https://irreactive.com/declarative-apis
Elm
1
star
24

School-Project

A Secure Dropbox-like server file management.
JavaScript
1
star
25

LambdaDeclarative

Little experiment on trying to implement a graphical Lambda-Calculus editor
Haskell
1
star
26

DontTouchTheSpikesHaskell

Don't touch the Spikes (Smartphone game, see Play Store) implemented in Haskell using DeclarativeGraphics
Haskell
1
star