UNPKG

6.17 kBMarkdownView Raw
1<h1 align="left">nean</h1>
2
3[![npm version][npm-src]][npm-href]
4[![types][types-src]][types-href]
5[![size][size-src]][size-href]
6[![coverage][coverage-src]][coverage-href]
7[![vulnerabilities][vulnerabilities-src]][vulnerabilities-href]
8[![dependencies][dep-src]][dep-href]
9[![devDependencies][devDep-src]][devDep-href]
10[![License][license-src]][license-href]
11
12> small kit for porting css frameworks to reactjs
13
14## Installation
15```bash
16$ npm i nean
17```
18
19## Usage
20```js
21import react, {Fragment} from 'react';
22import nean from 'nean';
23
24const Button = nean({
25 type: 'button',
26 className: 'btn',
27 style: ({rounded, primary}) => ({
28 'btn--rounded': rounded,
29 'btn--primary': primary,
30 }),
31 extend: ({md}) => ({
32 'data-size': md,
33 }),
34 render: ({children, prefix}) => (
35 <Fragment>
36 {prefix}{children}
37 </Fragment>
38 ),
39});
40
41<Button
42 prefix="foo"
43 md={2}
44 rounded
45 primary
46>
47 bar
48</Button>
49
50// ===
51
52<button
53 className="btn btn--rounded btn--primary"
54 data-size="2"
55>
56 foobar
57</button>
58```
59
60## API
61- <a href="#ctor"><code><b>nean()</b></code></a>
62- <a href="#interceptHook"><code>interceptHook()</code></a>
63- <a href="#createHook"><code>createHook()</code></a>
64- <a href="#useType"><code>useType()</code></a>
65
66<a name="library"></a>
67### Library
68
69<a name="ctor"></a>
70### `nean(config: Factory)`
71#### `Factory`
72| | type | default | description
73| :---------- | :---------| :------------| :----------
74| `type` | string | | type of element e.g. `span`, `div`, `button`
75| `className` | string? | | base className of the element
76| `style` | Function? | `props` | provides an object with all consumed props for translation
77| `extend` | Function? | `props` | allows extending or aliasing of props
78| `render` | Function? | `{children}` | overwrite of default render function
79
80##### `type`
81Pass type `button` to create a plain button component.
82```javascript
83import nean from 'nean';
84
85const Button = nean({
86 type: 'button',
87});
88```
89
90***
91
92##### `className`
93By adding `className`, the component receives a base className.
94```javascript
95import nean from 'nean';
96
97const Button = nean({
98 type: 'button',
99 className: 'btn',
100});
101```
102
103***
104
105##### `style(props)`
106To bind props to css classNames of the chosen framework, return an object with the new classNames with props as values.
107`style` is powered by the [@thomann/classnames](https://github.com/thomn/classnames) library
108and recursively evaluates every property/value by its truthiness and keeps its key.
109```javascript
110import nean from 'nean';
111
112const Button = nean({
113 type: 'button',
114 style: (({primary}) => ({
115 'btn-primary': primary
116 })),
117});
118```
119
120***
121
122##### `extend(props)`
123Sometimes, css frameworks have components which rely on data attributes.
124These can be set via `extend`. This function allows the extension of the original props.
125```javascript
126import nean from 'nean';
127
128const Button = nean({
129 type: 'button',
130 extend: (({badges}) => ({
131 'data-badges': badges
132 })),
133});
134```
135
136***
137
138##### `render(props)`
139It's possible to overwrite the render output via the `render` function.
140This allows to wrap your components `children` with other components.
141```javascript
142import React from 'react';
143import nean from 'nean';
144
145const Link = nean({
146 type: 'a',
147 render: (({children}) => (
148 <Button>
149 {children}
150 </Button>
151 )),
152});
153```
154
155***
156
157<a name="interceptHook"></a>
158### `interceptHook(use[, destructive = false])`
159nean components accept custom tailored hooks which can be addressed individually later on `render`.
160
161* `use` (array of hooks)
162* optional `destructive` (shift used hook from array of hooks)
163
164<a name="createHook"></a>
165### `createHook(name, hook)`
166hooks can extend a component via provided props.
167
168* `name` (name of the hook)
169* `hook` (hook function)
170
171```javascript
172import React from 'react';
173import nean, {interceptHook, createHook} from 'nean';
174
175// definition
176const Button = nean({
177 render: ({children, use}) => {
178 const icon = interceptHook(use)('icon');
179
180 return (
181 <Fragment>
182 {icon('left')} {children} {icon('right')}
183 </Fragment>
184 );
185 },
186});
187
188const useIcon = (type, side = 'left') => createHook('icon', (check) => {
189 if (check !== side) {
190 return null;
191 }
192
193 return (
194 <Icon type={type}/>
195 );
196});
197
198// usage
199const App = () => (
200 <Button use={[
201 useIcon('hamburger', 'right')
202 ]}>
203 hello world
204 </Button>
205)
206```
207
208***
209
210<a name="useType"></a>
211### `useType(type)`
212It's possible to overwrite the component type on runtime via the custom `useType` hook.
213
214* `type` (type or tag name of the element)
215
216```javascript
217import React from 'react';
218import nean, {useType} from 'nean';
219
220const App = () => (
221 <Button use={[useType('span')]}>
222 hello world, I am a span
223 </Button>
224);
225```
226
227## Licence
228MIT License, see [LICENSE](./LICENSE)
229
230[npm-src]: https://badgen.net/npm/v/nean
231[npm-href]: https://www.npmjs.com/package/nean
232[size-src]: https://badgen.net/packagephobia/install/nean
233[size-href]: https://packagephobia.com/result?p=nean
234[types-src]: https://badgen.net/npm/types/nean
235[types-href]: https://www.npmjs.com/package/nean
236[coverage-src]: https://coveralls.io/repos/github/sovrin/nean/badge.svg?branch=master
237[coverage-href]: https://coveralls.io/github/sovrin/nean?branch=master
238[vulnerabilities-src]: https://snyk.io/test/github/sovrin/nean/badge.svg
239[vulnerabilities-href]: https://snyk.io/test/github/sovrin/nean
240[dep-src]: https://badgen.net/david/dep/sovrin/nean
241[dep-href]: https://badgen.net/david/dep/sovrin/nean
242[devDep-src]: https://badgen.net/david/dev/sovrin/nean
243[devDep-href]: https://badgen.net/david/dev/sovrin/nean
244[license-src]: https://badgen.net/github/license/sovrin/nean
245[license-href]: LICENSE