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

gulp-typed-markup

v1.0.0

Published

Generate Typescript markup moduled from CSS

Downloads

5

Readme

gulp-typed-markup

This Gulp plugin takes CSS sources and generates Typescript files containing corresponding React element factories.

If you have the following element definition in CSS

/* Article.css */
.Article__caption {
    font-size: 28px;
}

the plugin will generate appropriate factory with binded CSS classes:

/* ArticleMarkup.ts */
import * as Markup from 'react-typed-markup';
declare var require: any; require('./Article.css');

export var caption: Markup.TagDIV<null> = Markup.bind(
    'div', 'Article', 'caption'
);

So, exported element factory can be used in React component's render code:

/* Article.ts */
import * as React from 'react';
import * as ArticleMarkup from './path-to-generated/ArticleMarkup';

export class Article extends React.Component<{}, {}> {
    render() {
        return ArticleMarkup.caption('Hello');
    }
}

No need to mess with string fragments to format CSS classes anymore. Just use autogenerated strictly typed factory functions.

Installation

$ npm install --save-dev gulp-typed-markup

Note that genearted markup modules also require react-typed-markup helper module. It exposes generic Typescirpt interfaces for element factories and contains wrapper function that invokes React.createElement under the hood. So install it too:

$ npm install --save react-typed-markup

BEM naming

We assume that you use BEM naming convention for CSS class names. But there is one exception: block is just a namespace for related elements, it must not contain any style props directly.

Instead of this:

/* wrong */
.Article {
    border: solid 2px;
}

always wrap block-level styles in a separated element:

/* ok */
.Article__article {
    border: solid 2px;
}

Second constraint: don't use dashes - in names, prefer camelCase style for Javascript compartibility.

Other things are the same, including, of course, modifiers.

Modifiers

Let's extend previously declared caption element with modifiers

.Article__caption {
    font-size: 28px;
}
.Article__caption_smallCaps {
    font-variant-caps: small-caps;
}
.Article__caption_type_info {
    color: blue;
}
.Article__caption_type_warning {
    color: red;
}

Generated Typescript fragment now looks like this

export var caption: Markup.TagDIV<CaptionMods> = Markup.bind(
    'div', 'Article', 'caption'
);
export type CaptionMods = {
    smallCaps?: boolean;
    type?: 'info' | 'warning';
}

Now modifiers can be used in your code

ArticleMarkup.caption({mods: {smallCaps: true, type: 'info'}}, 'Hello')

Passing React props to elements

It is also possible to pass regular React props within the first argrument:

ArticleMarkup.caption(
    {
        key: 42,
        onClick: () => (),
        mods: {smallCaps: true, type: 'info'}
    },
    'Hello'
)

Specifying tag name

Default generated tag is div. Just write specific tag name in css selector

h2.Article__caption {
    font-size: 28px;
}

Use empty rule, if you don't want to affect selector priority

h2.Article__caption {
}
.Article__caption {
    font-size: 28px;
}