Skip to content

Commit 0b2b26c

Browse files
committed
doc: integration guide for external libs
1 parent dfe0a3b commit 0b2b26c

File tree

9 files changed

+153
-55
lines changed

9 files changed

+153
-55
lines changed

docs/docs/howto/intro.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Demo
44

5-
```tsx twoslash
5+
```jsx twoslash
66
// @errors: 2322
77

88
function MyComponent({ children }) {
@@ -103,7 +103,7 @@ Configure your `tsconfig.json` as follows:
103103

104104
If you don't have any other JSX runtimes like React or Preact set up, you can use
105105
`typed-htmx/typed-html`, which will convert JSX into strings at runtime.
106-
You can configure the runtime using [`jsxConfig`](/typed-htmx/docs/api/module.index/Variables/variable.jsxConfig-1):
106+
You can configure the runtime using [`jsxConfig`](/typed-htmx/docs/api/index/variables/jsxConfig):
107107

108108
```js twoslash
109109
import { jsxConfig } from "typed-htmx";

docs/docs/howto/xternal.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Augmenting external JSX libraries
2+
3+
typed-htmx is extremely minimal and requires the user to manually augment external JSX libraries that provide their own types.
4+
5+
## Common guidance
6+
7+
- Create a `types.d.ts` (any name is fine, as long as it ends in `.d.ts`) at the top of your src/ folder,
8+
or anywhere within the configured `include` of your tsconfig.json
9+
- Write a JSX element, e.g. `<div />`, and inspect its type
10+
- If you see React-related types, you are good to go
11+
- If not, try to discover the common namespace under which all HTML attributes go.
12+
13+
Let's use [Hono](https://hono.dev/top) as an example.
14+
15+
```tsx twoslash
16+
// @jsxImportSource: hono/jsx
17+
// In tsconfig.json, jsxImportSource = "hono/jsx"
18+
19+
// The type we are augmenting in this case is `Hono.HTMLAttributes`.
20+
// hx-boost is not recognized as a proper attribute yet.
21+
<div hx-boost="bogus" />
22+
//^?
23+
```
24+
25+
With this knowledge, we can now augment the type of `Hono.HTMLAttributes` assuming it is an interface:
26+
27+
```tsx twoslash
28+
// @errors: 2322
29+
// @jsxImportSource: hono/jsx
30+
/// <reference types="typed-htmx" />
31+
32+
declare global {
33+
namespace Hono {
34+
interface HTMLAttributes extends HtmxAttributes {}
35+
}
36+
}
37+
38+
<div hx-boost="bogus"
39+
style={{}}
40+
/>
41+
```
42+
43+
## Hono
44+
45+
```ts twoslash
46+
import 'typed-htmx';
47+
48+
declare global {
49+
namespace Hono {
50+
interface HTMLAttributes extends HtmxAttributes {}
51+
}
52+
}
53+
```
54+
55+
## Astro
56+
57+
```ts twoslash
58+
import 'typed-htmx';
59+
60+
declare global {
61+
namespace astroHTML.JSX {
62+
interface IntrinsicAttributes extends HtmxAttributes {}
63+
}
64+
}
65+
```

docs/docusaurus.config.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const config = {
2121
markdown: {
2222
format: 'md'
2323
},
24-
onBrokenLinks: "throw",
25-
onBrokenMarkdownLinks: "ignore",
24+
onBrokenLinks: "warn",
25+
onBrokenMarkdownLinks: "warn",
2626

2727
i18n: {
2828
defaultLocale: "en",
@@ -36,12 +36,10 @@ const config = {
3636
({
3737
entryPoints: ["../src/index.ts", "../src/jsx.d.ts"],
3838
tsconfig: "../tsconfig.json",
39-
readme: "none",
4039
hideInPageTOC: true,
40+
readme: 'none',
4141
watch: process.env.npm_lifecycle_event === "start",
42-
// cleanOutputDir: process.env.NODE_ENV !== 'production',
43-
cleanOutputDir: false,
44-
excludeExternals: true,
42+
cleanOutputDir: true,
4543
externalPattern: ["node_modules/**/*"],
4644
plugin: ["typedoc-plugin-mdn-links"],
4745
}),
@@ -70,19 +68,17 @@ const config = {
7068
defaultOptions: {
7169
noErrors: false,
7270
},
73-
/** @type {import('typescript').CompilerOptions} */
7471
defaultCompilerOptions: {
75-
types: ["typed-htmx"],
7672
jsx: 4, // react-jsx
77-
jsxImportSource: "typed-htmx/typed-html",
73+
jsxImportSource: 'typed-htmx/typed-html',
7874
target: 99, // esnext,
7975
strict: true,
76+
checkJs: true,
8077
noImplicitAny: false,
8178
module: 199, // nodenext,
8279
moduleResolution: 99, // nodenext
8380
},
8481
includeJSDocInHover: true,
85-
wrapFragments: true,
8682
alwayRaiseForTwoslashExceptions: true,
8783
disableImplicitReactImport: true,
8884
}),

docs/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
"clsx": "^1.2.1",
2222
"docusaurus-plugin-typedoc": "next",
2323
"docusaurus-preset-shiki-twoslash": "^1.1.41",
24+
"hono": "^3.11.12",
2425
"object-assign": "^4.1.1",
2526
"prism-react-renderer": "^1.3.5",
2627
"react": "^18.2.0",
2728
"react-dom": "^18.2.0",
28-
"rehype-raw": "^7.0.0",
2929
"typed-htmx": "link:..",
3030
"typedoc": "^0.25.6",
3131
"typedoc-plugin-markdown": "next",
@@ -34,8 +34,7 @@
3434
},
3535
"devDependencies": {
3636
"@docusaurus/module-type-aliases": "3.0.1",
37-
"@docusaurus/types": "^3.0.1",
38-
"remark-shiki-twoslash": "^3.1.3"
37+
"@docusaurus/types": "^3.0.1"
3938
},
4039
"browserslist": {
4140
"production": [

docs/pnpm-lock.yaml

Lines changed: 28 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/typedoc.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
{
2-
"categoryOrder": ["core", "*"]
2+
"$schema": "https://typedoc.org/schema.json",
3+
"categoryOrder": ["core", "*"],
4+
"sourceLinkExternal": true,
5+
"excludeExternals": true,
6+
"externalPattern": [
7+
"**/node_modules/**/*"
8+
],
9+
"readme": "bogus",
10+
"searchCategoryBoosts": {
11+
"Core": 2
12+
}
313
}

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ function htmlTransformChildren(value: InterpValue): string {
9595
return out.join(" ");
9696
}
9797

98+
/**
99+
* A [tagged template](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates)
100+
* that interprets different kinds of {@link InterpValue values} into escaped HTML.
101+
*
102+
* ```ts twoslash
103+
* import { html } from 'typed-htmx';
104+
* function assertEqual(left: any, right: any) {}
105+
* // ---cut---
106+
* const template = html`
107+
* <div hx-vals=${{ foo: 'bar' }} />
108+
* `;
109+
* assertEqual(template, `<div hx-vals='{"foo":"bar"}' />`);
110+
* ```
111+
*/
98112
export const html: HtmlTemplator = (raw, ...values) => {
99113
const values_ = values.map(htmlTransformChildren);
100114
return String.raw(raw, ...values_);

0 commit comments

Comments
 (0)