Skip to content

Commit 6645dcf

Browse files
committed
Update to preact 6.1 and fix class/className massaging, fix class being left as an Object due to changes in Preact 6.1.
1 parent 5cad8c9 commit 6645dcf

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"chai": "^3.5.0",
4747
"eslint": "^3.2.2",
4848
"mocha": "^3.0.0",
49-
"preact": "^5.5.0",
49+
"preact": "^6.1.0",
5050
"rollup": "^0.34.3",
5151
"rollup-plugin-babel": "^2.6.1",
5252
"rollup-plugin-commonjs": "^3.3.1",

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { objectKeys, encodeEntities, falsey, memoize, indent, isLargeString, styleObjToCss, assign, getNodeProps } from './util';
1+
import { objectKeys, encodeEntities, falsey, memoize, indent, isLargeString, styleObjToCss, hashToClassName, assign, getNodeProps } from './util';
22

33
const SHALLOW = { shallow: true };
44

@@ -123,7 +123,11 @@ export default function renderToString(vnode, context, opts, inner) {
123123
if (attributes['class']) continue;
124124
name = 'class';
125125
}
126-
if (name==='style' && v && typeof v==='object') {
126+
127+
if (name==='class' && v && typeof v==='object') {
128+
v = hashToClassName(v);
129+
}
130+
else if (name==='style' && v && typeof v==='object') {
127131
v = styleObjToCss(v);
128132
}
129133

src/util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export let indent = (s, char) => String(s).replace(/(\n+)/g, '$1' + (char || '\t
3030

3131
export let isLargeString = (s, length, ignoreLines) => (String(s).length>(length || 40) || (!ignoreLines && String(s).indexOf('\n')!==-1) || String(s).indexOf('<')!==-1);
3232

33+
// Convert an Object style to a CSSText string
3334
export function styleObjToCss(s) {
3435
let str = '';
3536
for (let prop in s) {
@@ -48,6 +49,19 @@ export function styleObjToCss(s) {
4849
return str;
4950
}
5051

52+
53+
// See https://github.com/developit/preact/blob/master/src/util.js#L61
54+
export function hashToClassName(c) {
55+
let str = '';
56+
for (let prop in c) {
57+
if (c[prop]) {
58+
if (str) str += ' ';
59+
str += prop;
60+
}
61+
}
62+
return str;
63+
}
64+
5165
// Convert a JavaScript camel-case CSS property name to a CSS property name
5266
export let jsToCss = memoize( s => s.replace(/([A-Z])/g,'-$1').toLowerCase() );
5367

test/render.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,17 @@ describe('render', () => {
401401
expect(rendered).to.equal('<div class="foo bar"></div>');
402402
});
403403

404-
it('should prefer className over class', () => {
404+
it('should prefer class over className', () => {
405405
let rendered = render(<div class="foo" className="foo bar" />);
406-
expect(rendered).to.equal('<div class="foo bar"></div>');
406+
expect(rendered).to.equal('<div class="foo"></div>');
407+
});
408+
409+
it('should stringify object classNames', () => {
410+
let rendered = render(<div class={{ foo:1, bar:0, baz:true, buzz:false }} />);
411+
expect(rendered, 'class').to.equal('<div class="foo baz"></div>');
412+
413+
rendered = render(<div className={{ foo:1, bar:0, baz:true, buzz:false }} />);
414+
expect(rendered, 'className').to.equal('<div class="foo baz"></div>');
407415
});
408416
});
409417

0 commit comments

Comments
 (0)