Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shim for <Suspense /> #51

Merged
merged 2 commits into from
Mar 19, 2024
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $ npm install -S hyperons

This module provides two functions; one to create elements and one to render them. If you've worked with [React][react] or React-like libraries before then they're the equivalent to `React.createElement()` and `ReactDOM.renderToString()`.

The example below shows how to render a simple component using Hyperons:
The example below shows how to render a simple component using Hyperons with vanilla JavaScript:

[react]: https://reactjs.org/

Expand Down Expand Up @@ -129,6 +129,22 @@ const DescriptionList = () => {
}
```

### `Hyperons.Suspense`

`Suspense` is a special component which renders a fallback for lazy-loaded or async children. Hyperons only renders static HTML so this component exists only for compatibility purposes and will not render its children.

```jsx
import { h, Suspense } from 'hyperons'

const AsyncComponent = () => {
return (
<Suspense fallback={<Loading />}>
<SomeComponent />
</Suspense>
)
}
```

### `Hyperons.createContext`

Creates a new [context](#context) object. Components which subscribe to this context will read the current context value from the closest matching context provider above it in the tree. Hyperons largely supports the same [context API](https://reactjs.org/docs/context.html) as React including accessing context via the `Class.contextType` property and `useContext()` [hook](#hooks).
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import Component from './component'
import createContext from './context'
import createElement from './create-element'
import Fragment from './fragment'
import Suspense from './suspense'
import renderToString from './render-to-string'

export * from './hooks'

export {
Fragment,
Suspense,
Component,
createContext,
createElement,
Expand Down
5 changes: 5 additions & 0 deletions src/render-to-string.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import stringifyStyles from './stringify-styles'
import escapeString from './escape-string'
import Fragment from './fragment'
import Suspense from './suspense'
import dispatcher from './dispatcher'

const ATTR_ALIASES = {
Expand Down Expand Up @@ -130,6 +131,10 @@ function renderToString(element, context = {}) {
return renderToString(props.children, context)
}

if (type === Suspense) {
return renderToString(props.fallback, context)
}

if (typeof type === 'string') {
let html = `<${type}`

Expand Down
2 changes: 2 additions & 0 deletions src/suspense.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const Suspense = Symbol('Suspense')
export default Suspense
13 changes: 13 additions & 0 deletions test/async.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, it } from 'vitest'
import { h, render, Suspense } from '../src'

describe('async', () => {
it('renders the provided fallback component', () => {
const result = render(
<Suspense fallback={<p>Yes</p>}>
<p>No</p>
</Suspense>
)
expect(result).toBe('<p>Yes</p>')
})
})
Loading