@modular-component/with-fragment 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # @modular-component/with-fragment
2
+
3
+ ## 0.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 77c46f4: Fix an issue in core rendering system
8
+ - Updated dependencies [77c46f4]
9
+ - @modular-component/core@0.2.2
package/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright © Jérémie van der Sande
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4
+ documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
5
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
6
+ persons to whom the Software is furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+
10
+ The Software is provided “as is”, without warranty of any kind, express or implied, including but not limited to the
11
+ warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or
12
+ copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise,
13
+ arising from, out of or in connection with the software or the use or other dealings in the Software.
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # @modular-component/with-fragment
2
+
3
+ ModularComponent stage allowing to cut the render phase into multiple fragments that can be composed together in the
4
+ render stage. It can make conditional rendering easier to read and reduce duplication.
5
+
6
+ It can either return a map of React node accessible through the `fragments` field, or be called multiple times by
7
+ specifying a field key to use and returning a single React node.
8
+
9
+ ## Usage
10
+
11
+ ```tsx
12
+ import { ModularComponent, render } from '@modular-component/core'
13
+ import { fragment } from '@modular-component/with-fragment'
14
+
15
+ // Through multiple calls
16
+ const MultipleCalls = ModularComponent<{ loading: boolean }>()
17
+ .with(fragment('loading', () => <div>Loading...</div>))
18
+ .with(fragment('loaded', () => <div>Loaded</div>))
19
+ .with(render(({ props, loading, loaded }) => (
20
+ <div>Current status: {props.loading ? loading : loaded}</div>
21
+ )))
22
+
23
+ // Through a single call
24
+ const SingleCall = ModularComponent<{ loading: boolean }>()
25
+ .with(fragment(() => ({
26
+ loading: <div>Loading...</div>,
27
+ loaded: <div>Loaded</div>,
28
+ }))
29
+ .with(render(({ props, fragments }) => (
30
+ <div>Current status: {props.loading ? fragments.loading : fragments.loaded}</div>
31
+ ))))
32
+ ```
33
+
34
+ ## Implementation
35
+
36
+ `with(fragment)` receives a function taking the current arguments map as parameter and returns either a map of React node
37
+ or a single React node, to set as stage function, and optionally a key to be used as the field.
38
+
39
+ ```ts
40
+ import { ReactNode } from 'react'
41
+ import { ModularStage } from '@modular-component/core'
42
+
43
+ export function fragment<
44
+ Args extends {},
45
+ Fragments extends Record<string, ReactNode>,
46
+ >(
47
+ useFragment: (args: Args) => Fragments,
48
+ ): ModularStage<'fragments', (args: Args) => Fragments>
49
+ export function fragment<
50
+ Args extends {},
51
+ Fragment extends ReactNode,
52
+ Key extends string,
53
+ >(
54
+ key: Key,
55
+ useFragment: (args: Args) => Fragment,
56
+ ): ModularStage<Key, (args: Args) => Fragment>
57
+
58
+ export function fragment<Key extends string, Stage extends () => unknown>(
59
+ key: Key | Stage,
60
+ useFragment?: Stage,
61
+ ): ModularStage<Key, Stage> {
62
+ return {
63
+ field: (typeof key === 'string' ? key : 'fragments') as Key,
64
+ useStage: (typeof key === 'string' ? useFragment : key) as Stage,
65
+ }
66
+ }
67
+ ```
@@ -0,0 +1,5 @@
1
+ import { ReactNode } from 'react';
2
+ import { ModularStage } from '@modular-component/core';
3
+ export declare function fragment<Args extends {}, Fragments extends Record<string, ReactNode>>(useFragment: (args: Args) => Fragments): ModularStage<'fragments', (args: Args) => Fragments>;
4
+ export declare function fragment<Args extends {}, Fragment extends ReactNode, Key extends string>(key: Key, useFragment: (args: Args) => Fragment): ModularStage<Key, (args: Args) => Fragment>;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAEtD,wBAAgB,QAAQ,CACtB,IAAI,SAAS,EAAE,EACf,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAE3C,WAAW,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,SAAS,GACrC,YAAY,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,SAAS,CAAC,CAAA;AACvD,wBAAgB,QAAQ,CACtB,IAAI,SAAS,EAAE,EACf,QAAQ,SAAS,SAAS,EAC1B,GAAG,SAAS,MAAM,EAElB,GAAG,EAAE,GAAG,EACR,WAAW,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,QAAQ,GACpC,YAAY,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export function fragment(key, useFragment) {
2
+ return {
3
+ field: (typeof key === 'string' ? key : 'fragments'),
4
+ useStage: (typeof key === 'string' ? useFragment : key),
5
+ };
6
+ }
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAkBA,MAAM,UAAU,QAAQ,CACtB,GAAgB,EAChB,WAAmB;IAEnB,OAAO;QACL,KAAK,EAAE,CAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,WAAW,CAAQ;QAC3D,QAAQ,EAAE,CAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,CAAU;KACjE,CAAA;AACH,CAAC"}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@modular-component/with-fragment",
3
+ "description": "ModularComponent stage for handling render fragments",
4
+ "keywords": [
5
+ "React",
6
+ "Modular",
7
+ "Factory",
8
+ "Default",
9
+ "Conditional"
10
+ ],
11
+ "version": "0.2.2",
12
+ "type": "module",
13
+ "license": "MIT",
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "files": [
18
+ "src",
19
+ "dist",
20
+ "CHANGELOG.md"
21
+ ],
22
+ "scripts": {
23
+ "build": "yarn build:core && yarn build:self",
24
+ "build:core": "yarn workspace @modular-component/core build",
25
+ "build:self": "tsc -p tsconfig.build.json",
26
+ "license": "cp ../../LICENSE ./LICENSE"
27
+ },
28
+ "dependencies": {
29
+ "@modular-component/core": "0.2.2"
30
+ },
31
+ "peerDependencies": {
32
+ "react": ">=17 <19"
33
+ },
34
+ "devDependencies": {
35
+ "@types/react": "^18.0.17",
36
+ "typescript": "^5.2.2"
37
+ },
38
+ "main": "dist/index.js",
39
+ "types": "dist/index.d.ts"
40
+ }
package/src/index.ts ADDED
@@ -0,0 +1,27 @@
1
+ import { ReactNode } from 'react'
2
+ import { ModularStage } from '@modular-component/core'
3
+
4
+ export function fragment<
5
+ Args extends {},
6
+ Fragments extends Record<string, ReactNode>,
7
+ >(
8
+ useFragment: (args: Args) => Fragments,
9
+ ): ModularStage<'fragments', (args: Args) => Fragments>
10
+ export function fragment<
11
+ Args extends {},
12
+ Fragment extends ReactNode,
13
+ Key extends string,
14
+ >(
15
+ key: Key,
16
+ useFragment: (args: Args) => Fragment,
17
+ ): ModularStage<Key, (args: Args) => Fragment>
18
+
19
+ export function fragment<Key extends string, Stage extends () => unknown>(
20
+ key: Key | Stage,
21
+ useFragment?: Stage,
22
+ ): ModularStage<Key, Stage> {
23
+ return {
24
+ field: (typeof key === 'string' ? key : 'fragments') as Key,
25
+ useStage: (typeof key === 'string' ? useFragment : key) as Stage,
26
+ }
27
+ }