Skip to content

Commit 501ab72

Browse files
authored
feat: add support for nested StringParamsProvider (#548)
1 parent 808857b commit 501ab72

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

.changeset/plucky-birds-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@blinkk/root": patch
3+
---
4+
5+
fix: merge params from nested StringParamsProvider

packages/root/src/core/hooks/useStringParams.ts renamed to packages/root/src/core/hooks/useStringParams.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {createContext} from 'preact';
1+
import {ComponentChildren, FunctionalComponent, createContext} from 'preact';
22
import {useContext} from 'preact/hooks';
33

44
export type StringParamsContext = Record<string, string>;
@@ -7,7 +7,21 @@ export const STRING_PARAMS_CONTEXT = createContext<StringParamsContext | null>(
77
null
88
);
99

10-
export const StringParamsProvider = STRING_PARAMS_CONTEXT.Provider;
10+
export interface StringParamsProviderProps {
11+
value?: StringParamsContext;
12+
children?: ComponentChildren;
13+
}
14+
15+
export const StringParamsProvider: FunctionalComponent<StringParamsProviderProps> = (props) => {
16+
// Allow for nested param values from parent content providers.
17+
const parent = useContext(STRING_PARAMS_CONTEXT) || {};
18+
const merged = {...parent, ...props.value};
19+
return (
20+
<STRING_PARAMS_CONTEXT.Provider value={merged}>
21+
{props.children}
22+
</STRING_PARAMS_CONTEXT.Provider>
23+
);
24+
};
1125

1226
/**
1327
* A hook that returns a map of string params, configured via the
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
prettyHtml: true,
3+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {StringParamsProvider, useTranslations} from '../../../../dist/core';
2+
3+
export default function Page() {
4+
const t = useTranslations();
5+
return (
6+
<StringParamsProvider value={{foo: 'foovalue'}}>
7+
<StringParamsProvider value={{bar: 'barvalue'}}>
8+
<p>{t('{foo} / {bar}')}</p>
9+
</StringParamsProvider>
10+
</StringParamsProvider>
11+
);
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {promises as fs} from 'node:fs';
2+
import path from 'node:path';
3+
import {beforeEach, afterEach, expect, test, assert} from 'vitest';
4+
import {fileExists} from '../src/utils/fsutils.js';
5+
import {Fixture, loadFixture} from './testutils.js';
6+
7+
let fixture: Fixture;
8+
9+
beforeEach(async () => {
10+
fixture = await loadFixture('./fixtures/string-params');
11+
});
12+
13+
afterEach(async () => {
14+
if (fixture) {
15+
await fixture.cleanup();
16+
}
17+
});
18+
19+
test('merge nested StringParamsProvider values', async () => {
20+
await fixture.build();
21+
const indexPath = path.join(fixture.distDir, 'html/index.html');
22+
assert.isTrue(await fileExists(indexPath));
23+
const html = await fs.readFile(indexPath, 'utf-8');
24+
expect(html).toMatchInlineSnapshot(`
25+
"<!doctype html>
26+
<html>
27+
<head>
28+
<meta charset=\"utf-8\">
29+
</head>
30+
<body>
31+
<p>foovalue / barvalue</p>
32+
</body>
33+
</html>
34+
"
35+
`);
36+
});

0 commit comments

Comments
 (0)