Skip to content

Commit 89b1618

Browse files
jeetissshuding
andauthored
fix: update yoga and types (#429)
Co-authored-by: Shu Ding <[email protected]>
1 parent 995ab71 commit 89b1618

File tree

7 files changed

+31
-63
lines changed

7 files changed

+31
-63
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
"@types/node": "^16",
7777
"@types/opentype.js": "^1.3.3",
7878
"@types/react": "^17.0.38",
79-
"@types/yoga-layout": "^1.9.4",
8079
"@typescript-eslint/eslint-plugin": "^5.40.0",
8180
"@typescript-eslint/parser": "^5.40.0",
8281
"@vitest/ui": "^0.7.6",
@@ -104,7 +103,7 @@
104103
"emoji-regex": "^10.2.1",
105104
"linebreak": "^1.1.0",
106105
"postcss-value-parser": "^4.2.0",
107-
"yoga-wasm-web": "^0.3.1"
106+
"yoga-wasm-web": "^0.3.3"
108107
},
109108
"packageManager": "[email protected]",
110109
"engines": {

pnpm-lock.yaml

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

src/handler/index.ts

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* also returns the inherited style for children of the element.
55
*/
66

7-
import type { YogaNode } from 'yoga-layout'
7+
import type { Node as YogaNode } from 'yoga-wasm-web'
88

99
import getYoga from '../yoga/index.js'
1010
import presets from './presets.js'
@@ -274,35 +274,21 @@ export default async function handler(
274274
)
275275
)
276276

277-
// @TODO: fix types
278-
if (
279-
// @ts-ignore
280-
typeof node.setGap !== 'undefined' &&
281-
// @ts-ignore
282-
typeof node.getGap !== 'undefined'
283-
) {
284-
if (typeof style.gap !== 'undefined') {
285-
// @ts-ignore
286-
node.setGap(Yoga.GUTTER_ALL, style.gap)
287-
}
277+
if (typeof style.gap !== 'undefined') {
278+
node.setGap(Yoga.GUTTER_ALL, style.gap as number)
279+
}
288280

289-
if (typeof style.rowGap !== 'undefined') {
290-
// @ts-ignore
291-
node.setGap(Yoga.GUTTER_ROW, style.rowGap)
292-
}
281+
if (typeof style.rowGap !== 'undefined') {
282+
node.setGap(Yoga.GUTTER_ROW, style.rowGap as number)
283+
}
293284

294-
if (typeof style.columnGap !== 'undefined') {
295-
// @ts-ignore
296-
node.setGap(Yoga.GUTTER_COLUMN, style.columnGap)
297-
}
285+
if (typeof style.columnGap !== 'undefined') {
286+
node.setGap(Yoga.GUTTER_COLUMN, style.columnGap as number)
298287
}
299288

300289
// @TODO: node.setFlex
301290

302291
if (typeof style.flexBasis !== 'undefined') {
303-
// We can't use `auto` here due to this:
304-
// https://github.com/facebook/yoga/pull/1112
305-
// @TODO: We need a fork to add this API.
306292
node.setFlexBasis(style.flexBasis)
307293
}
308294
node.setFlexGrow(
@@ -337,10 +323,10 @@ export default async function handler(
337323
)
338324
)
339325

340-
node.setMargin(Yoga.EDGE_TOP, (style.marginTop as number) || 0)
341-
node.setMargin(Yoga.EDGE_BOTTOM, (style.marginBottom as number) || 0)
342-
node.setMargin(Yoga.EDGE_LEFT, (style.marginLeft as number) || 0)
343-
node.setMargin(Yoga.EDGE_RIGHT, (style.marginRight as number) || 0)
326+
node.setMargin(Yoga.EDGE_TOP, style.marginTop || 0)
327+
node.setMargin(Yoga.EDGE_BOTTOM, style.marginBottom || 0)
328+
node.setMargin(Yoga.EDGE_LEFT, style.marginLeft || 0)
329+
node.setMargin(Yoga.EDGE_RIGHT, style.marginRight || 0)
344330

345331
node.setBorder(Yoga.EDGE_TOP, (style.borderTopWidth as number) || 0)
346332
node.setBorder(Yoga.EDGE_BOTTOM, (style.borderBottomWidth as number) || 0)

src/layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import type { ReactNode } from 'react'
6-
import type { YogaNode } from 'yoga-layout'
6+
import type { Node as YogaNode } from 'yoga-wasm-web'
77

88
import getYoga from './yoga/index.js'
99
import {

src/yoga/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
let Yoga: typeof import('yoga-layout')
1+
import type { Yoga } from 'yoga-wasm-web'
22

3-
export function init(yoga: typeof Yoga) {
4-
Yoga = yoga
3+
let yogaInstance: Yoga
4+
5+
export function init(yoga: Yoga) {
6+
yogaInstance = yoga
57
}
68

79
let initializationPromise = null
810

9-
export default async function getYoga(): Promise<typeof Yoga> {
10-
if (Yoga) return Yoga
11+
export default async function getYoga(): Promise<Yoga> {
12+
if (yogaInstance) return yogaInstance
1113

1214
if (initializationPromise) {
1315
await initializationPromise
14-
return Yoga
16+
return yogaInstance
1517
}
1618

1719
initializationPromise = import('@yoga')
1820
.then((mod) => mod.getYogaModule())
19-
.then((yogaInstance) => (Yoga = yogaInstance))
21+
.then((yoga) => (yogaInstance = yoga))
2022

2123
await initializationPromise
2224
initializationPromise = null
2325

24-
return Yoga
26+
return yogaInstance
2527
}

src/yoga/yoga-prebuilt.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
export async function getYogaModule() {
2-
const initYoga = await import('yoga-wasm-web/asm')
3-
if (initYoga.default) {
4-
return initYoga.default()
5-
}
2+
const { default: initYoga } = await import('yoga-wasm-web/asm')
63
return initYoga()
74
}

test/utils.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@ import { join } from 'path'
33
import { Resvg } from '@resvg/resvg-js'
44
import { toMatchImageSnapshot } from 'jest-image-snapshot'
55
import { readFile } from 'node:fs/promises'
6-
import initYoga from 'yoga-wasm-web'
6+
import yoga from 'yoga-wasm-web/auto'
77

88
import { init, type SatoriOptions } from '../src/index.js'
99

1010
export function initYogaWasm() {
1111
beforeAll(async () => {
12-
const yoga = await initYoga(
13-
await readFile(
14-
join(
15-
require.resolve('yoga-wasm-web/package.json'),
16-
'..',
17-
'dist',
18-
'yoga.wasm'
19-
)
20-
)
21-
)
2212
init(yoga)
2313
})
2414
}

0 commit comments

Comments
 (0)