Skip to content

Commit

Permalink
feat: initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
vesparny committed Apr 20, 2017
1 parent 3937d4e commit f6ef554
Show file tree
Hide file tree
Showing 13 changed files with 4,404 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"presets": ["env"],
"plugins": [
["transform-react-jsx", { "pragma":"h" }],
"transform-object-rest-spread"
]
}
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true
[*]
# Change these settings to your own preference
indent_style = space
indent_size = 2
# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/dist
/node_modules
/npm-debug.log
.DS_Store
coverage
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: node_js
node_js:
- 6
cache:
directories:
- node_modules
notifications:
email: false
script:
- yarn run test
after_success:
- npm install -g codecov
- codecov
branches:
only:
- master
Empty file added CHANGELOG.md
Empty file.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Alessandro Arnodo
Copyright (c) 2017-present Alessandro Arnodo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# preact-glam

> A tiny glamorous implementation for preact
[![Travis](https://img.shields.io/travis/vesparny/preact-glam.svg)](https://travis-ci.org/vesparny/preact-glam)
[![David](https://img.shields.io/david/vesparny/preact-glam.svg)](https://david-dm.org/vesparny/preact-glam)
[![npm](https://img.shields.io/npm/v/preact-glam.svg)](https://www.npmjs.com/package/preact-glam)
[![npm](https://img.shields.io/npm/dm/preact-glam.svg)](https://npm-stat.com/charts.html?package=preact-glam&from=2017-04-01)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
[![MIT License](https://img.shields.io/npm/l/preact-glam.svg?style=flat-square)](https://github.com/vesparny/preact-glam/blob/master/LICENSE)

## Install

This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.

```sh
$ yarn add preact-glam
```

Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else:

```javascript
// using ES6 modules
import glam from 'preact-glam'

// using CommonJS modules
var glam = require('preact-glam')
```

The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):

```html
<script src="https://unpkg.com/preact-glam/dist/preact-glam.umd.js"></script>
```

You can find the library on `window.preactGlam`.

## Usage

Please refer to [glamorous-tiny](https://github.com/paypal/glamorous#size)

## Tests

```sh
$ yarn run test
```


[MIT License](LICENSE.md) © [Alessandro Arnodo](https://alessandro.arnodo.net/)
92 changes: 92 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"name": "preact-glam",
"amdName": "preactGlam",
"version": "0.0.0",
"description": "A tiny glamorous implementation for preact",
"jsnext:main": "dist/preact-glam.es.js",
"module": "dist/preact-glam.es.js",
"main": "dist/preact-glam.cjs.js",
"umd:main": "dist/preact-glam.umd.js",
"scripts": {
"precommit": "lint-staged",
"bump": "standard-version",
"testonly": "jest --coverage",
"lint": "standard",
"test": "npm run lint && npm run testonly",
"build": "npm-run-all test clean rollup rollup:min size",
"clean": "rimraf dist",
"rollup": "rollup -c",
"rollup:min": "cross-env MINIFY=minify rollup -c",
"size": "echo \"Gzipped Size: $(cat dist/preact-glam.umd.min.js | gzip-size)\"",
"release": "npm run build && npm run bump && git push --follow-tags origin master && npm publish"
},
"repository": "vesparny/preact-glam",
"keywords": [
"preact",
"glamor",
"glamorous",
"glam",
"css",
"style"
],
"homepage": "https://github.com/vesparny/preact-glam",
"authors": [
"Alessandro Arnodo <[email protected]>"
],
"license": "MIT",
"files": [
"dist",
"src",
"test"
],
"devDependencies": {
"babel-core": "^6.24.1",
"babel-eslint": "^7.2.2",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-plugin-transform-react-jsx": "^6.24.1",
"cross-env": "^4.0.0",
"glamor": "^2.20.24",
"gzip-size-cli": "^2.0.0",
"husky": "^0.13.3",
"jest": "^19.0.2",
"lint-staged": "^3.4.0",
"npm-run-all": "^4.0.2",
"preact": "^8.1.0",
"prettier": "^1.2.2",
"rimraf": "^2.5.2",
"rollup": "^0.41.4",
"rollup-plugin-babel": "^2.7.1",
"rollup-plugin-uglify": "^1.0.1",
"standard": "^10.0.2",
"standard-version": "^4.0.0"
},
"dependencies": {
"babel-preset-env": "^1.4.0"
},
"peerDependencies": {
"glamor": "^2.20.24",
"preact": "^8.1.0"
},
"jest": {
"roots": [
"src"
]
},
"standard": {
"parser": "babel-eslint",
"globals": [
"jest",
"expect",
"it",
"test",
"describe"
]
},
"lint-staged": {
"*.js": [
"prettier --write --semi false",
"standard --fix",
"git add"
]
}
}
65 changes: 65 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import babel from 'rollup-plugin-babel'
import uglify from 'rollup-plugin-uglify'

const minify = !!process.env.MINIFY

export default {
entry: 'src/index.js',
moduleName: 'preactGlam',
useStrict: false,
sourceMap: minify,
external: ['preact', 'glamor'],
globals: {
preact: 'preact',
glamor: 'Glamor'
},
plugins: [
babel({
exclude: 'node_modules/**',
babelrc: false,
presets: [
[
'env',
{
modules: false
}
]
],
plugins: [
[
'transform-react-jsx',
{
pragma: 'h'
}
],
'transform-object-rest-spread'
]
}),
minify ? uglify() : {}
],
targets: minify
? [
{
dest: 'dist/preact-glam.umd.min.js',
format: 'umd'
},
{
dest: 'dist/preact-glam.cjs.min.js',
format: 'cjs'
}
]
: [
{
dest: 'dist/preact-glam.cjs.js',
format: 'cjs'
},
{
dest: 'dist/preact-glam.es.js',
format: 'es'
},
{
dest: 'dist/preact-glam.umd.js',
format: 'umd'
}
]
}
9 changes: 9 additions & 0 deletions src/__test__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @jsx h */
import glam from '../index'

test('should pass glam object prop', () => {
const Comp = glam('div')({
marginLeft: 1
})
expect(Comp).toBeDefined()
})
31 changes: 31 additions & 0 deletions src/get-classname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { css, styleSheet } from 'glamor'

function extractGlamorStyles (className = '') {
return className.toString().split(' ').reduce((groups, name) => {
if (name.indexOf('css-') === 0) {
const id = name.slice('css-'.length)
const { style } = styleSheet.registered[id]
groups.glamorStyles.push(style)
} else {
groups.glamorlessClassName = `${groups.glamorlessClassName} ${name}`.trim()
}
return groups
}, { glamorlessClassName: '', glamorStyles: [] })
}

export default getGlamorClassName

function getGlamorClassName (styles, props) {
const mappedArgs = styles.slice()
for (let i = mappedArgs.length; i--;) {
if (typeof mappedArgs[i] === 'function') {
mappedArgs[i] = mappedArgs[i](props)
}
}
const {
glamorStyles: parentGlamorStyles,
glamorlessClassName
} = extractGlamorStyles(props.className)
const glamorClassName = css(...mappedArgs, ...parentGlamorStyles).toString()
return `${glamorlessClassName} ${glamorClassName}`.trim()
}
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** @jsx h */
import { h } from 'preact'
import getClassName from './get-classname'

export default function glam (comp) {
return function glamComponentFactory (...styles) {
const GlamComponent = props => {
const { className, glam, ...toForward } = props
const fullClassName = getClassName(GlamComponent.styles, props)
return <GlamComponent.comp {...toForward} className={fullClassName} />
}

GlamComponent.comp = comp.comp ? comp.comp : comp
GlamComponent.styles = comp.styles ? comp.styles.concat(styles) : styles
return GlamComponent
}
}
Loading

0 comments on commit f6ef554

Please sign in to comment.