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

TypeScript definitions for 2.0.8 #969

Closed
wants to merge 50 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
ba0f953
Add TypeScript definitions.
icylace Jul 21, 2020
286debf
Nitpick: Rename constant for clarity
hanneskaeufler Jul 25, 2020
26312af
Merge pull request #970 from hanneskaeufler/patch-1
jorgebucaran Jul 26, 2020
6f0ba80
Update `VNode`.
icylace Jul 27, 2020
d405ddf
Use `text(...)` instead of plain `string` in tutorial.
xieyuheng Jul 28, 2020
3b606b5
Merge pull request #973 from xieyuheng/master
jorgebucaran Jul 28, 2020
dcd4155
Let `ClassProp` know when a class is turned off.
icylace Aug 17, 2020
c717670
Refactor while making a couple convenience types.
icylace Aug 18, 2020
b127089
Allow effects to unsubscribe from subscriptions.
icylace Aug 18, 2020
3da5b09
Fix minor issues in reference.md
coldfix Aug 23, 2020
7231ece
Fix some outdated text in tutorial.md
coldfix Aug 23, 2020
89bc769
Merge pull request #978 from coldfix/doc-fixes
jorgebucaran Aug 23, 2020
f338799
Clean up some code style
icylace Aug 26, 2020
73175f2
Adjust some code style.
icylace Sep 3, 2020
e4d5919
Simplify type variable usage.
icylace Sep 3, 2020
e76dcfd
Allow effects to be asynchronous.
icylace Sep 3, 2020
7d00455
Update to include definitions for events.
icylace Sep 3, 2020
ea60391
Separate state transitions from action nesting.
icylace Sep 4, 2020
a5a99cb
Only allow functions be used for event handling.
icylace Sep 5, 2020
c7f013e
Overhaul type definitions and start adding tests.
icylace Sep 14, 2020
839367b
Tweak code style.
icylace Sep 15, 2020
5b3e647
Refine `style` property and update tests.
icylace Sep 15, 2020
2b17454
Start adding effect type tests.
icylace Sep 15, 2020
a964fdf
Add type tests for async effects.
icylace Sep 15, 2020
447c141
Add type tests for middleware.
icylace Sep 16, 2020
b0c3b60
Tweak type tests for effects.
icylace Sep 16, 2020
6e53553
Add type tests for actions.
icylace Sep 16, 2020
105bea1
Refactor `Action`.
icylace Sep 17, 2020
d548400
Reorder types.
icylace Sep 17, 2020
3a21823
Unify `Payload` with `EffectData` and add flexibility.
icylace Sep 21, 2020
0f9432a
Update type tests.
icylace Sep 21, 2020
2c5af2b
Update some types.
icylace Sep 24, 2020
3952928
Merge remote-tracking branch 'upstream/master'
icylace Nov 14, 2020
3180dda
Tweak some minor things.
icylace Nov 14, 2020
2486626
Let `ClassProp` have `undefined`.
icylace Nov 14, 2020
02685cb
Tweak some things.
icylace Nov 15, 2020
4f74af8
Include TypeScript definition when installed as a dependency.
icylace Nov 15, 2020
271bf37
Work on types.
icylace Nov 15, 2020
3c2ac5b
Tweak how the .d.ts file is referenced.
icylace Nov 15, 2020
11f79e9
Fix `main` prop in package.json.
icylace Nov 16, 2020
149358f
Tweak linting.
icylace Nov 29, 2020
4e9f5a6
Fix the type definition for the `app`.
icylace Nov 29, 2020
2e7c905
Take advantage of template literal types.
icylace Dec 7, 2020
5ec4882
Improve perspective of state.
icylace Dec 28, 2020
bb36e75
Tweak wording and layout.
icylace Dec 28, 2020
874777d
Update some type tests.
icylace Dec 28, 2020
be5ecfb
Add some notes and to the type definitions.
icylace Jan 22, 2021
b505373
Fix a type bug and add some type tests.
icylace Jan 22, 2021
e11e1ce
Introduce a type-level only guard prop to check for VDOM objects.
icylace Jan 23, 2021
0b7049f
Overhaul type terminology.
icylace Jan 23, 2021
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
6 changes: 3 additions & 3 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Below is a consice recap of Hyperapp's core APIs and packages. It's geared towar
## h()

```js
h(type, props, ...children)
h(type, props, children)
```

Hyperscript function to create virtual DOM nodes (VNodes), which are used for [rendering](#rendering).
Expand All @@ -33,7 +33,7 @@ A VNode is a simplified representation of an HTML element. It represents an elem

- **type** - Name of the node, eg: div, h1, button, etc.
- **props** - Object containing HTML or SVG attributes the DOM node will have and [special props](#special-props).
- **children** - Array of child VNodes.
- **children** - Array of child VNodes (optional).

```js
import { h, text } from "hyperapp"
Expand Down Expand Up @@ -322,7 +322,7 @@ const httpFx = (dispatch, props) => {
// Do side effects
fetch(props.url, props.options)
.then((res) => res.json())
.then((data) => dispatch(data)) // Optionnally dispatch an action
.then((data) => dispatch(props.action, data)) // Optionally dispatch an action
}

// Helper to easily create the effect tuple for the Http effect
Expand Down
126 changes: 61 additions & 65 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tutorial

Welcome! If you're new to Hyperapp, you've found the perfect place to start learning. This tutorial will guide you through your first steps with Hyperapp as we build a simple app.
Welcome! If you're new to Hyperapp, you've found the perfect place to start learning. This tutorial will guide you through your first steps with Hyperapp as we build a simple app.

- [The Set-up](#setup)
- [Hello World](#helloworld)
Expand Down Expand Up @@ -101,7 +101,7 @@ Create this html file:
href="https://hyperapp.dev/tutorial-assets/style.css"
/>
<script type="module">
import { h, app } from "https://unpkg.com/hyperapp"
import { h, text, app } from "https://unpkg.com/hyperapp"

// -- EFFECTS & SUBSCRIPTIONS --

Expand All @@ -111,8 +111,9 @@ Create this html file:

// -- RUN --
app({
init: {},
node: document.getElementById("app"),
view: () => h("h1", {}, ["Hello ", h("i", {}, "World!")]),
view: () => h("h1", {}, [text("Hello "), h("i", {}, text("World!"))]),
})
</script>
</head>
Expand Down Expand Up @@ -141,7 +142,7 @@ which _represent_ DOM nodes.
The result of

```js
h("h1", {}, ["Hello ", h("i", {}, "World!")])
h("h1", {}, [text("Hello "), h("i", {}, text("World!"))])
```

is a virtual node, representing
Expand All @@ -167,51 +168,51 @@ To render the HTML we want, change the `view` to:
view: () =>
h("div", { id: "app", class: "container" }, [
h("div", { class: "filter" }, [
" Filter: ",
h("span", { class: "filter-word" }, "ocean"),
h("button", {}, "\u270E"),
text(" Filter: "),
h("span", { class: "filter-word" }, text("ocean")),
h("button", {}, text("\u270E")),
]),
h("div", { class: "stories" }, [
h("ul", {}, [
h("li", { class: "unread" }, [
h("p", { class: "title" }, [
"The ",
h("em", {}, "Ocean"),
" is Sinking!",
text("The "),
h("em", {}, text("Ocean")),
text(" is Sinking!"),
]),
h("p", { class: "author" }, "Kat Stropher"),
h("p", { class: "author" }, text("Kat Stropher")),
]),
h("li", { class: "reading" }, [
h("p", { class: "title" }, [h("em", {}, "Ocean"), " life is brutal"]),
h("p", { class: "author" }, "Surphy McBrah"),
h("p", { class: "title" }, [h("em", {}, text("Ocean")), text(" life is brutal")]),
h("p", { class: "author" }, text("Surphy McBrah")),
]),
h("li", {}, [
h("p", { class: "title" }, [
"Family friendly fun at the ",
h("em", {}, "ocean"),
" exhibit",
text("Family friendly fun at the "),
h("em", {}, text("ocean")),
text(" exhibit"),
]),
h("p", { class: "author" }, "Guy Prosales"),
h("p", { class: "author" }, text("Guy Prosales")),
]),
]),
]),
h("div", { class: "story" }, [
h("h1", {}, "Ocean life is brutal"),
h("h1", {}, text("Ocean life is brutal")),
h(
"p",
{},
`
text(`
Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat.
`
`)
),
h("p", { class: "signature" }, "Surphy McBrah"),
h("p", { class: "signature" }, text("Surphy McBrah")),
]),
h("div", { class: "autoupdate" }, [
"Auto update: ",
text("Auto update: "),
h("input", { type: "checkbox" }),
]),
])
Expand Down Expand Up @@ -239,7 +240,7 @@ const emphasize = (word, string) =>
string
.split(" ")
.map((x) =>
x.toLowerCase() === word.toLowerCase() ? h("em", {}, x + " ") : x + " "
x.toLowerCase() === word.toLowerCase() ? h("em", {}, text(x + " ")) : text(x + " ")
)
```

Expand All @@ -248,9 +249,9 @@ It lets you change this:
```js
...
h("p", {class: "title"}, [
"The ",
h("em", {}, "Ocean"),
" is Sinking!"
text("The "),
h("em", {}, text("Ocean")),
text(" is Sinking!")
]),
...
```
Expand All @@ -261,7 +262,7 @@ into this:
...
h("p", {class: "title"}, emphasize("ocean",
"The Ocean is Sinking"
))
)),
...
```

Expand All @@ -280,7 +281,7 @@ const storyThumbnail = (props) =>
},
[
h("p", { class: "title" }, emphasize(props.filter, props.title)),
h("p", { class: "author" }, props.author),
h("p", { class: "author" }, text(props.author)),
]
)
```
Expand Down Expand Up @@ -312,32 +313,32 @@ const storyList = (props) =>

const filterView = (props) =>
h("div", { class: "filter" }, [
"Filter:",
h("span", { class: "filter-word" }, props.filter),
h("button", {}, "\u270E"),
text("Filter:"),
h("span", { class: "filter-word" }, text(props.filter)),
h("button", {}, text("\u270E")),
])

const storyDetail = (props) =>
h("div", { class: "story" }, [
props && h("h1", {}, props.title),
props && h("h1", {}, text(props.title)),
props &&
h(
"p",
{},
`
h(
"p",
{},
text(`
Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, qui
nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat.
`
),
props && h("p", { class: "signature" }, props.author),
`)
),
props && h("p", { class: "signature" }, text(props.author)),
])

const autoUpdateView = (props) =>
h("div", { class: "autoupdate" }, [
"Auto update: ",
text("Auto update: "),
h("input", { type: "checkbox" }),
])

Expand Down Expand Up @@ -444,9 +445,9 @@ Add an `onclick` property to the button in `filterView`:
```js
const filterView = (props) =>
h("div", { class: "filter" }, [
"Filter:",
h("span", { class: "filter-word" }, props.filter),
h("button", { onclick: StartEditingFilter }, "\u270E"), // <---
text("Filter:"),
h("span", { class: "filter-word" }, text(props.filter)),
h("button", { onclick: StartEditingFilter }, text("\u270E")), // <---
])
```

Expand All @@ -473,13 +474,13 @@ ternary operator (`a ? b : c`).
```js
const filterView = (props) =>
h("div", { class: "filter" }, [
"Filter:",
text("Filter:"),

props.editingFilter // <---
? h("input", { type: "text", value: props.filter }) // <---
: h("span", { class: "filter-word" }, props.filter),
: h("span", { class: "filter-word" }, text(props.filter)),

h("button", { onclick: StartEditingFilter }, "\u270E"),
h("button", { onclick: StartEditingFilter }, text("\u270E")),
])
```

Expand All @@ -497,15 +498,15 @@ and update `filterView` again:
```js
const filterView = (props) =>
h("div", { class: "filter" }, [
"Filter:",
text("Filter:"),

props.editingFilter
? h("input", { type: "text", value: props.filter })
: h("span", { class: "filter-word" }, props.filter),
: h("span", { class: "filter-word" }, text(props.filter)),

props.editingFilter // <---
? h("button", { onclick: StopEditingFilter }, "\u2713")
: h("button", { onclick: StartEditingFilter }, "\u270E"), // <---
? h("button", { onclick: StopEditingFilter }, text("\u2713"))
: h("button", { onclick: StartEditingFilter }, text("\u270E")), // <---
])
```

Expand All @@ -523,19 +524,19 @@ Update `filterView` yet again:
```js
const filterView = (props) =>
h("div", { class: "filter" }, [
"Filter:",
text("Filter:"),

props.editingFilter
? h("input", {
type: "text",
value: props.filter,
oninput: SetFilter, // <----
})
: h("span", { class: "filter-word" }, props.filter),
: h("span", { class: "filter-word" }, text(props.filter)),

props.editingFilter
? h("button", { onclick: StopEditingFilter }, "\u2713")
: h("button", { onclick: StartEditingFilter }, "\u270E"),
? h("button", { onclick: StopEditingFilter }, text("\u2713"))
: h("button", { onclick: StartEditingFilter }, text("\u270E")),
])
```

Expand Down Expand Up @@ -579,7 +580,7 @@ const storyThumbnail = (props) =>
},
[
h("p", { class: "title" }, emphasize(props.filter, props.title)),
h("p", { class: "author" }, props.author),
h("p", { class: "author" }, text(props.author)),
]
)
```
Expand Down Expand Up @@ -629,21 +630,16 @@ const filterView = (props) =>
? h("input", {
type: "text",
value: props.filter,
oninput: [SetFilter, (event) => event.target.value], // <----
oninput: (state, event) => SetFilter(state, event.target.value), // <----
})
: h("span", { class: "filter-word" }, props.filter),
: h("span", { class: "filter-word" }, text(props.filter)),

props.editingFilter
? h("button", { onclick: StopEditingFilter }, "\u2713")
: h("button", { onclick: StartEditingFilter }, "\u270E"),
? h("button", { onclick: StopEditingFilter }, text("\u2713"))
: h("button", { onclick: StartEditingFilter }, text("\u270E")),
])
```

When we give a _function_ as the custom payload, Hyperapp considers it a _payload filter_ and passes the default
payload through it, providing the returned value as payload to the action.

> Payload filters are also useful when you need a payload that is a combination of custom data and event data

If you'd like to see a working example of the code so far, have a look [here](https://codesandbox.io/s/hyperapp-tutorial-step-2-5yv34)

## Effects <a name="effects"></a>
Expand Down Expand Up @@ -917,7 +913,7 @@ Dispatch it in response to checking the checkbox in `autoUpdateView`:
```js
const autoUpdateView = (props) =>
h("div", { class: "autoupdate" }, [
"Auto update: ",
text("Auto update: "),
h("input", {
type: "checkbox",
checked: props.autoUpdate, // <---
Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"description": "The tiny framework for building hypertext applications",
"version": "2.0.8",
"type": "module",
"main": "hyperapp.js",
"main": "index.js",
"license": "MIT",
"repository": "jorgebucaran/hyperapp",
"homepage": "https://hyperapp.dev",
"files": [
"*.js*"
"*.js*",
"types/index.d.ts"
],
"author": "Jorge Bucaran",
"funding": "https://github.com/sponsors/jorgebucaran",
Expand All @@ -27,11 +28,15 @@
"minify": "terser ${prefix}index.js -o ${prefix}$out -mc --source-map url=$out.map --module",
"create": "npm run build && git commit -am $msg && git tag -s $msg -m $msg && git push && git push --tags",
"message": "echo ${pkg:+@$npm_package_name/$pkg@}$(node -p \"require('./${pkg:+pkg/$pkg/}package').version\")",
"release": "env msg=$(npm run -s message) npm run create && cd ./${pkg:+pkg/$pkg} && npm publish --access public"
"release": "env msg=$(npm run -s message) npm run create && cd ./${pkg:+pkg/$pkg} && npm publish --access public",
"dts": "dtslint types"
},
"devDependencies": {
"c8": "*",
"dtslint": "*",
"terser": "*",
"twist": "*",
"terser": "*"
}
"typescript": "*"
},
"types": "types/index.d.ts"
}
4 changes: 2 additions & 2 deletions pkg/html/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const EMPTY_ARR = []
const NO_CHILDREN = []

const h = (type) => (props, children = EMPTY_ARR) => ({
const h = (type) => (props, children = NO_CHILDREN) => ({
type,
props,
children: Array.isArray(children) ? children : [children],
Expand Down
Loading