Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mean-dragons-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix support for signals, we need to detect and unwrap the signal
16 changes: 14 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"@babel/register": "^7.12.10",
"@changesets/changelog-github": "^0.4.1",
"@changesets/cli": "^2.18.0",
"@preact/signals-core": "^1.11.0",
"baseline-rts": "npm:preact-render-to-string@latest",
"benchmarkjs-pretty": "^2.0.1",
"check-export-map": "^1.3.1",
Expand Down
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SVG_CAMEL_CASE,
createComponent
} from './lib/util.js';
import { Signal } from '@preact/signals-core';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this was accidentally left around, will need to be removed but I gotta get to bed -- I can do it tomorrow but wanted to drop a mention at least so we don't release this.

import { options, h, Fragment } from 'preact';
import {
CHILDREN,
Expand Down Expand Up @@ -559,6 +560,7 @@ function _renderToString(

for (let name in props) {
let v = props[name];
v = isSignal(v) ? v.value : v;

if (typeof v == 'function' && name !== 'class' && name !== 'className') {
continue;
Expand Down Expand Up @@ -740,3 +742,12 @@ const SELF_CLOSING = new Set([
export default renderToString;
export const render = renderToString;
export const renderToStaticMarkup = renderToString;

function isSignal(x) {
return (
x !== null &&
typeof x === 'object' &&
typeof x.peek === 'function' &&
'value' in x
);
}
7 changes: 7 additions & 0 deletions test/render.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ describe('render', () => {
expect(rendered).to.equal(expected);
});

it('should include boolean disabled attribute', () => {
let rendered = render(<input disabled={false} />),
expected = `<input/>`;

expect(rendered).to.equal(expected);
});

it('should support false aria-* attributes', () => {
let rendered = render(<div aria-checked={false} />);
expect(rendered).to.equal(`<div aria-checked="false"></div>`);
Expand Down
16 changes: 16 additions & 0 deletions test/signals/render.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import render from '../../src/index.js';
import { signal } from '@preact/signals-core';
import { h } from 'preact';
import { expect, describe, it } from 'vitest';

/** @jsx h */

describe('signals', () => {
it('should render signals', () => {
const disabled = signal(false);

const vdom = <input draggable={false} disabled={disabled} />;

expect(render(vdom)).to.equal('<input draggable="false"/>');
});
});