@tamagui/adapt 1.0.1-rc.1.4 → 1.0.1-rc.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/dist/cjs/Adapt.js +87 -0
- package/dist/cjs/Adapt.js.map +7 -0
- package/dist/cjs/index.js +2 -67
- package/dist/cjs/index.js.map +3 -3
- package/dist/esm/Adapt.js +66 -0
- package/dist/esm/Adapt.js.map +7 -0
- package/dist/esm/index.js +1 -62
- package/dist/esm/index.js.map +3 -3
- package/package.json +3 -3
- package/src/Adapt.tsx +80 -0
- package/src/index.tsx +1 -75
- package/types/Adapt.d.ts +32 -0
- package/types/index.d.ts +1 -25
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var Adapt_exports = {};
|
|
20
|
+
__export(Adapt_exports, {
|
|
21
|
+
Adapt: () => Adapt,
|
|
22
|
+
AdaptContents: () => AdaptContents,
|
|
23
|
+
useAdaptParent: () => useAdaptParent
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(Adapt_exports);
|
|
26
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
27
|
+
var import_core = require("@tamagui/core");
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
const AdaptParentContext = (0, import_react.createContext)(null);
|
|
30
|
+
const AdaptContents = (props) => {
|
|
31
|
+
const context = (0, import_react.useContext)(AdaptParentContext);
|
|
32
|
+
if (!context || !context.Contents) {
|
|
33
|
+
throw new Error(`Adapt not supported by this component`);
|
|
34
|
+
}
|
|
35
|
+
return (0, import_react.createElement)(context.Contents, props);
|
|
36
|
+
};
|
|
37
|
+
AdaptContents["shouldForwardSpace"] = true;
|
|
38
|
+
const useAdaptParent = ({ Contents }) => {
|
|
39
|
+
const [when, setWhen] = (0, import_react.useState)(null);
|
|
40
|
+
const AdaptProvider = (0, import_react.useMemo)(() => {
|
|
41
|
+
const context = {
|
|
42
|
+
Contents,
|
|
43
|
+
setWhen
|
|
44
|
+
};
|
|
45
|
+
return (props) => {
|
|
46
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AdaptParentContext.Provider, {
|
|
47
|
+
value: context,
|
|
48
|
+
children: props.children
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
}, [Contents]);
|
|
52
|
+
return {
|
|
53
|
+
AdaptProvider,
|
|
54
|
+
when
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
const Adapt = (0, import_core.withStaticProperties)(
|
|
58
|
+
function Adapt2({ platform, when, children }) {
|
|
59
|
+
const context = (0, import_react.useContext)(AdaptParentContext);
|
|
60
|
+
let enabled = !platform;
|
|
61
|
+
if (platform === "touch")
|
|
62
|
+
enabled = import_core.isTouchable;
|
|
63
|
+
if (platform === "native")
|
|
64
|
+
enabled = !import_core.isWeb;
|
|
65
|
+
if (platform === "web")
|
|
66
|
+
enabled = import_core.isWeb;
|
|
67
|
+
(0, import_core.useIsomorphicLayoutEffect)(() => {
|
|
68
|
+
if (!enabled)
|
|
69
|
+
return;
|
|
70
|
+
context == null ? void 0 : context.setWhen(when);
|
|
71
|
+
}, [when, context, enabled]);
|
|
72
|
+
if (!enabled) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
return children;
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
Contents: AdaptContents
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
82
|
+
0 && (module.exports = {
|
|
83
|
+
Adapt,
|
|
84
|
+
AdaptContents,
|
|
85
|
+
useAdaptParent
|
|
86
|
+
});
|
|
87
|
+
//# sourceMappingURL=Adapt.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/Adapt.tsx"],
|
|
4
|
+
"sourcesContent": ["import {\n MediaQueryKey,\n isTouchable,\n isWeb,\n useIsomorphicLayoutEffect,\n withStaticProperties,\n} from '@tamagui/core'\nimport { createContext, createElement, useContext, useMemo, useState } from 'react'\n\nexport type AdaptProps = {\n when?: MediaQueryKey\n platform?: 'native' | 'web' | 'touch'\n children?: any\n}\n\ntype Component = (props: any) => any\ntype AdaptParentContextI = {\n Contents: Component\n setWhen: (when: MediaQueryKey) => any\n}\n\nconst AdaptParentContext = createContext<AdaptParentContextI | null>(null)\n\n// forward props\nexport const AdaptContents = (props: any) => {\n const context = useContext(AdaptParentContext)\n if (!context || !context.Contents) {\n throw new Error(`Adapt not supported by this component`)\n }\n return createElement(context.Contents, props)\n}\n\nAdaptContents['shouldForwardSpace'] = true\n\nexport const useAdaptParent = ({ Contents }: { Contents: AdaptParentContextI['Contents'] }) => {\n const [when, setWhen] = useState<MediaQueryKey | null>(null)\n\n const AdaptProvider = useMemo(() => {\n const context: AdaptParentContextI = {\n Contents,\n setWhen,\n }\n\n return (props: { children?: any }) => {\n return (\n <AdaptParentContext.Provider value={context}>{props.children}</AdaptParentContext.Provider>\n )\n }\n }, [Contents])\n\n return {\n AdaptProvider,\n when,\n }\n}\n\nexport const Adapt = withStaticProperties(\n function Adapt({ platform, when, children }: AdaptProps) {\n const context = useContext(AdaptParentContext)\n\n let enabled = !platform\n if (platform === 'touch') enabled = isTouchable\n if (platform === 'native') enabled = !isWeb\n if (platform === 'web') enabled = isWeb\n\n useIsomorphicLayoutEffect(() => {\n if (!enabled) return\n context?.setWhen(when as MediaQueryKey)\n }, [when, context, enabled])\n\n if (!enabled) {\n return null\n }\n\n return children\n },\n {\n Contents: AdaptContents,\n }\n)\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6CQ;AA7CR,kBAMO;AACP,mBAA4E;AAc5E,MAAM,yBAAqB,4BAA0C,IAAI;AAGlE,MAAM,gBAAgB,CAAC,UAAe;AAC3C,QAAM,cAAU,yBAAW,kBAAkB;AAC7C,MAAI,CAAC,WAAW,CAAC,QAAQ,UAAU;AACjC,UAAM,IAAI,MAAM,uCAAuC;AAAA,EACzD;AACA,aAAO,4BAAc,QAAQ,UAAU,KAAK;AAC9C;AAEA,cAAc,wBAAwB;AAE/B,MAAM,iBAAiB,CAAC,EAAE,SAAS,MAAqD;AAC7F,QAAM,CAAC,MAAM,OAAO,QAAI,uBAA+B,IAAI;AAE3D,QAAM,oBAAgB,sBAAQ,MAAM;AAClC,UAAM,UAA+B;AAAA,MACnC;AAAA,MACA;AAAA,IACF;AAEA,WAAO,CAAC,UAA8B;AACpC,aACE,4CAAC,mBAAmB,UAAnB;AAAA,QAA4B,OAAO;AAAA,QAAU,gBAAM;AAAA,OAAS;AAAA,IAEjE;AAAA,EACF,GAAG,CAAC,QAAQ,CAAC;AAEb,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;AAEO,MAAM,YAAQ;AAAA,EACnB,SAASA,OAAM,EAAE,UAAU,MAAM,SAAS,GAAe;AACvD,UAAM,cAAU,yBAAW,kBAAkB;AAE7C,QAAI,UAAU,CAAC;AACf,QAAI,aAAa;AAAS,gBAAU;AACpC,QAAI,aAAa;AAAU,gBAAU,CAAC;AACtC,QAAI,aAAa;AAAO,gBAAU;AAElC,+CAA0B,MAAM;AAC9B,UAAI,CAAC;AAAS;AACd,yCAAS,QAAQ;AAAA,IACnB,GAAG,CAAC,MAAM,SAAS,OAAO,CAAC;AAE3B,QAAI,CAAC,SAAS;AACZ,aAAO;AAAA,IACT;AAEA,WAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,UAAU;AAAA,EACZ;AACF;",
|
|
6
|
+
"names": ["Adapt"]
|
|
7
|
+
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -3,10 +3,6 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
6
|
var __copyProps = (to, from, except, desc) => {
|
|
11
7
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
8
|
for (let key of __getOwnPropNames(from))
|
|
@@ -15,70 +11,9 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
11
|
}
|
|
16
12
|
return to;
|
|
17
13
|
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
15
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
16
|
var src_exports = {};
|
|
20
|
-
__export(src_exports, {
|
|
21
|
-
Adapt: () => Adapt,
|
|
22
|
-
AdaptContents: () => AdaptContents,
|
|
23
|
-
useAdaptParent: () => useAdaptParent
|
|
24
|
-
});
|
|
25
17
|
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
|
|
27
|
-
var import_core = require("@tamagui/core");
|
|
28
|
-
var import_react = require("react");
|
|
29
|
-
const AdaptParentContext = (0, import_react.createContext)(null);
|
|
30
|
-
const AdaptContents = () => {
|
|
31
|
-
const context = (0, import_react.useContext)(AdaptParentContext);
|
|
32
|
-
if (!context || !context.Contents) {
|
|
33
|
-
throw new Error(`Missing parent adapatable component, Adapt not supported here`);
|
|
34
|
-
}
|
|
35
|
-
return (0, import_react.createElement)(context.Contents);
|
|
36
|
-
};
|
|
37
|
-
const useAdaptParent = ({ Contents }) => {
|
|
38
|
-
const [when, setWhen] = (0, import_react.useState)(null);
|
|
39
|
-
const AdaptProvider = (0, import_react.useMemo)(() => {
|
|
40
|
-
const context = {
|
|
41
|
-
Contents,
|
|
42
|
-
setWhen
|
|
43
|
-
};
|
|
44
|
-
return (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AdaptParentContext.Provider, {
|
|
45
|
-
value: context,
|
|
46
|
-
children: props.children
|
|
47
|
-
});
|
|
48
|
-
}, [Contents]);
|
|
49
|
-
return {
|
|
50
|
-
AdaptProvider,
|
|
51
|
-
when
|
|
52
|
-
};
|
|
53
|
-
};
|
|
54
|
-
const Adapt = (0, import_core.withStaticProperties)(
|
|
55
|
-
function Adapt2({ platform, when, children }) {
|
|
56
|
-
const context = (0, import_react.useContext)(AdaptParentContext);
|
|
57
|
-
let enabled = !platform;
|
|
58
|
-
if (platform === "touch")
|
|
59
|
-
enabled = import_core.isTouchable;
|
|
60
|
-
if (platform === "native")
|
|
61
|
-
enabled = !import_core.isWeb;
|
|
62
|
-
if (platform === "web")
|
|
63
|
-
enabled = import_core.isWeb;
|
|
64
|
-
(0, import_core.useIsomorphicLayoutEffect)(() => {
|
|
65
|
-
if (!enabled)
|
|
66
|
-
return;
|
|
67
|
-
context == null ? void 0 : context.setWhen(when);
|
|
68
|
-
}, [when, context, enabled]);
|
|
69
|
-
if (!enabled) {
|
|
70
|
-
return null;
|
|
71
|
-
}
|
|
72
|
-
return children;
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
Contents: AdaptContents
|
|
76
|
-
}
|
|
77
|
-
);
|
|
78
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
79
|
-
0 && (module.exports = {
|
|
80
|
-
Adapt,
|
|
81
|
-
AdaptContents,
|
|
82
|
-
useAdaptParent
|
|
83
|
-
});
|
|
18
|
+
__reExport(src_exports, require("./Adapt"), module.exports);
|
|
84
19
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.tsx"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
4
|
+
"sourcesContent": ["export * from './Adapt'\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,oBAAd;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import {
|
|
3
|
+
isTouchable,
|
|
4
|
+
isWeb,
|
|
5
|
+
useIsomorphicLayoutEffect,
|
|
6
|
+
withStaticProperties
|
|
7
|
+
} from "@tamagui/core";
|
|
8
|
+
import { createContext, createElement, useContext, useMemo, useState } from "react";
|
|
9
|
+
const AdaptParentContext = createContext(null);
|
|
10
|
+
const AdaptContents = (props) => {
|
|
11
|
+
const context = useContext(AdaptParentContext);
|
|
12
|
+
if (!context || !context.Contents) {
|
|
13
|
+
throw new Error(`Adapt not supported by this component`);
|
|
14
|
+
}
|
|
15
|
+
return createElement(context.Contents, props);
|
|
16
|
+
};
|
|
17
|
+
AdaptContents["shouldForwardSpace"] = true;
|
|
18
|
+
const useAdaptParent = ({ Contents }) => {
|
|
19
|
+
const [when, setWhen] = useState(null);
|
|
20
|
+
const AdaptProvider = useMemo(() => {
|
|
21
|
+
const context = {
|
|
22
|
+
Contents,
|
|
23
|
+
setWhen
|
|
24
|
+
};
|
|
25
|
+
return (props) => {
|
|
26
|
+
return /* @__PURE__ */ jsx(AdaptParentContext.Provider, {
|
|
27
|
+
value: context,
|
|
28
|
+
children: props.children
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
}, [Contents]);
|
|
32
|
+
return {
|
|
33
|
+
AdaptProvider,
|
|
34
|
+
when
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
const Adapt = withStaticProperties(
|
|
38
|
+
function Adapt2({ platform, when, children }) {
|
|
39
|
+
const context = useContext(AdaptParentContext);
|
|
40
|
+
let enabled = !platform;
|
|
41
|
+
if (platform === "touch")
|
|
42
|
+
enabled = isTouchable;
|
|
43
|
+
if (platform === "native")
|
|
44
|
+
enabled = !isWeb;
|
|
45
|
+
if (platform === "web")
|
|
46
|
+
enabled = isWeb;
|
|
47
|
+
useIsomorphicLayoutEffect(() => {
|
|
48
|
+
if (!enabled)
|
|
49
|
+
return;
|
|
50
|
+
context == null ? void 0 : context.setWhen(when);
|
|
51
|
+
}, [when, context, enabled]);
|
|
52
|
+
if (!enabled) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return children;
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
Contents: AdaptContents
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
export {
|
|
62
|
+
Adapt,
|
|
63
|
+
AdaptContents,
|
|
64
|
+
useAdaptParent
|
|
65
|
+
};
|
|
66
|
+
//# sourceMappingURL=Adapt.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/Adapt.tsx"],
|
|
4
|
+
"sourcesContent": ["import {\n MediaQueryKey,\n isTouchable,\n isWeb,\n useIsomorphicLayoutEffect,\n withStaticProperties,\n} from '@tamagui/core'\nimport { createContext, createElement, useContext, useMemo, useState } from 'react'\n\nexport type AdaptProps = {\n when?: MediaQueryKey\n platform?: 'native' | 'web' | 'touch'\n children?: any\n}\n\ntype Component = (props: any) => any\ntype AdaptParentContextI = {\n Contents: Component\n setWhen: (when: MediaQueryKey) => any\n}\n\nconst AdaptParentContext = createContext<AdaptParentContextI | null>(null)\n\n// forward props\nexport const AdaptContents = (props: any) => {\n const context = useContext(AdaptParentContext)\n if (!context || !context.Contents) {\n throw new Error(`Adapt not supported by this component`)\n }\n return createElement(context.Contents, props)\n}\n\nAdaptContents['shouldForwardSpace'] = true\n\nexport const useAdaptParent = ({ Contents }: { Contents: AdaptParentContextI['Contents'] }) => {\n const [when, setWhen] = useState<MediaQueryKey | null>(null)\n\n const AdaptProvider = useMemo(() => {\n const context: AdaptParentContextI = {\n Contents,\n setWhen,\n }\n\n return (props: { children?: any }) => {\n return (\n <AdaptParentContext.Provider value={context}>{props.children}</AdaptParentContext.Provider>\n )\n }\n }, [Contents])\n\n return {\n AdaptProvider,\n when,\n }\n}\n\nexport const Adapt = withStaticProperties(\n function Adapt({ platform, when, children }: AdaptProps) {\n const context = useContext(AdaptParentContext)\n\n let enabled = !platform\n if (platform === 'touch') enabled = isTouchable\n if (platform === 'native') enabled = !isWeb\n if (platform === 'web') enabled = isWeb\n\n useIsomorphicLayoutEffect(() => {\n if (!enabled) return\n context?.setWhen(when as MediaQueryKey)\n }, [when, context, enabled])\n\n if (!enabled) {\n return null\n }\n\n return children\n },\n {\n Contents: AdaptContents,\n }\n)\n"],
|
|
5
|
+
"mappings": "AA6CQ;AA7CR;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,eAAe,eAAe,YAAY,SAAS,gBAAgB;AAc5E,MAAM,qBAAqB,cAA0C,IAAI;AAGlE,MAAM,gBAAgB,CAAC,UAAe;AAC3C,QAAM,UAAU,WAAW,kBAAkB;AAC7C,MAAI,CAAC,WAAW,CAAC,QAAQ,UAAU;AACjC,UAAM,IAAI,MAAM,uCAAuC;AAAA,EACzD;AACA,SAAO,cAAc,QAAQ,UAAU,KAAK;AAC9C;AAEA,cAAc,wBAAwB;AAE/B,MAAM,iBAAiB,CAAC,EAAE,SAAS,MAAqD;AAC7F,QAAM,CAAC,MAAM,OAAO,IAAI,SAA+B,IAAI;AAE3D,QAAM,gBAAgB,QAAQ,MAAM;AAClC,UAAM,UAA+B;AAAA,MACnC;AAAA,MACA;AAAA,IACF;AAEA,WAAO,CAAC,UAA8B;AACpC,aACE,oBAAC,mBAAmB,UAAnB;AAAA,QAA4B,OAAO;AAAA,QAAU,gBAAM;AAAA,OAAS;AAAA,IAEjE;AAAA,EACF,GAAG,CAAC,QAAQ,CAAC;AAEb,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;AAEO,MAAM,QAAQ;AAAA,EACnB,SAASA,OAAM,EAAE,UAAU,MAAM,SAAS,GAAe;AACvD,UAAM,UAAU,WAAW,kBAAkB;AAE7C,QAAI,UAAU,CAAC;AACf,QAAI,aAAa;AAAS,gBAAU;AACpC,QAAI,aAAa;AAAU,gBAAU,CAAC;AACtC,QAAI,aAAa;AAAO,gBAAU;AAElC,8BAA0B,MAAM;AAC9B,UAAI,CAAC;AAAS;AACd,yCAAS,QAAQ;AAAA,IACnB,GAAG,CAAC,MAAM,SAAS,OAAO,CAAC;AAE3B,QAAI,CAAC,SAAS;AACZ,aAAO;AAAA,IACT;AAEA,WAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,UAAU;AAAA,EACZ;AACF;",
|
|
6
|
+
"names": ["Adapt"]
|
|
7
|
+
}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,63 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
isTouchable,
|
|
4
|
-
isWeb,
|
|
5
|
-
useIsomorphicLayoutEffect,
|
|
6
|
-
withStaticProperties
|
|
7
|
-
} from "@tamagui/core";
|
|
8
|
-
import { createContext, createElement, useContext, useMemo, useState } from "react";
|
|
9
|
-
const AdaptParentContext = createContext(null);
|
|
10
|
-
const AdaptContents = () => {
|
|
11
|
-
const context = useContext(AdaptParentContext);
|
|
12
|
-
if (!context || !context.Contents) {
|
|
13
|
-
throw new Error(`Missing parent adapatable component, Adapt not supported here`);
|
|
14
|
-
}
|
|
15
|
-
return createElement(context.Contents);
|
|
16
|
-
};
|
|
17
|
-
const useAdaptParent = ({ Contents }) => {
|
|
18
|
-
const [when, setWhen] = useState(null);
|
|
19
|
-
const AdaptProvider = useMemo(() => {
|
|
20
|
-
const context = {
|
|
21
|
-
Contents,
|
|
22
|
-
setWhen
|
|
23
|
-
};
|
|
24
|
-
return (props) => /* @__PURE__ */ jsx(AdaptParentContext.Provider, {
|
|
25
|
-
value: context,
|
|
26
|
-
children: props.children
|
|
27
|
-
});
|
|
28
|
-
}, [Contents]);
|
|
29
|
-
return {
|
|
30
|
-
AdaptProvider,
|
|
31
|
-
when
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
|
-
const Adapt = withStaticProperties(
|
|
35
|
-
function Adapt2({ platform, when, children }) {
|
|
36
|
-
const context = useContext(AdaptParentContext);
|
|
37
|
-
let enabled = !platform;
|
|
38
|
-
if (platform === "touch")
|
|
39
|
-
enabled = isTouchable;
|
|
40
|
-
if (platform === "native")
|
|
41
|
-
enabled = !isWeb;
|
|
42
|
-
if (platform === "web")
|
|
43
|
-
enabled = isWeb;
|
|
44
|
-
useIsomorphicLayoutEffect(() => {
|
|
45
|
-
if (!enabled)
|
|
46
|
-
return;
|
|
47
|
-
context == null ? void 0 : context.setWhen(when);
|
|
48
|
-
}, [when, context, enabled]);
|
|
49
|
-
if (!enabled) {
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
return children;
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
Contents: AdaptContents
|
|
56
|
-
}
|
|
57
|
-
);
|
|
58
|
-
export {
|
|
59
|
-
Adapt,
|
|
60
|
-
AdaptContents,
|
|
61
|
-
useAdaptParent
|
|
62
|
-
};
|
|
1
|
+
export * from "./Adapt";
|
|
63
2
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.tsx"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
4
|
+
"sourcesContent": ["export * from './Adapt'\n"],
|
|
5
|
+
"mappings": "AAAA,cAAc;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/adapt",
|
|
3
|
-
"version": "1.0.1-rc.
|
|
3
|
+
"version": "1.0.1-rc.2",
|
|
4
4
|
"types": "./types/index.d.ts",
|
|
5
5
|
"main": "dist/cjs",
|
|
6
6
|
"module": "dist/esm",
|
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
"clean:build": "tamagui-build clean:build"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@tamagui/core": "^1.0.1-rc.
|
|
21
|
+
"@tamagui/core": "^1.0.1-rc.2"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@tamagui/build": "^1.0.1-rc.
|
|
24
|
+
"@tamagui/build": "^1.0.1-rc.2"
|
|
25
25
|
},
|
|
26
26
|
"publishConfig": {
|
|
27
27
|
"access": "public"
|
package/src/Adapt.tsx
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MediaQueryKey,
|
|
3
|
+
isTouchable,
|
|
4
|
+
isWeb,
|
|
5
|
+
useIsomorphicLayoutEffect,
|
|
6
|
+
withStaticProperties,
|
|
7
|
+
} from '@tamagui/core'
|
|
8
|
+
import { createContext, createElement, useContext, useMemo, useState } from 'react'
|
|
9
|
+
|
|
10
|
+
export type AdaptProps = {
|
|
11
|
+
when?: MediaQueryKey
|
|
12
|
+
platform?: 'native' | 'web' | 'touch'
|
|
13
|
+
children?: any
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type Component = (props: any) => any
|
|
17
|
+
type AdaptParentContextI = {
|
|
18
|
+
Contents: Component
|
|
19
|
+
setWhen: (when: MediaQueryKey) => any
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const AdaptParentContext = createContext<AdaptParentContextI | null>(null)
|
|
23
|
+
|
|
24
|
+
// forward props
|
|
25
|
+
export const AdaptContents = (props: any) => {
|
|
26
|
+
const context = useContext(AdaptParentContext)
|
|
27
|
+
if (!context || !context.Contents) {
|
|
28
|
+
throw new Error(`Adapt not supported by this component`)
|
|
29
|
+
}
|
|
30
|
+
return createElement(context.Contents, props)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
AdaptContents['shouldForwardSpace'] = true
|
|
34
|
+
|
|
35
|
+
export const useAdaptParent = ({ Contents }: { Contents: AdaptParentContextI['Contents'] }) => {
|
|
36
|
+
const [when, setWhen] = useState<MediaQueryKey | null>(null)
|
|
37
|
+
|
|
38
|
+
const AdaptProvider = useMemo(() => {
|
|
39
|
+
const context: AdaptParentContextI = {
|
|
40
|
+
Contents,
|
|
41
|
+
setWhen,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return (props: { children?: any }) => {
|
|
45
|
+
return (
|
|
46
|
+
<AdaptParentContext.Provider value={context}>{props.children}</AdaptParentContext.Provider>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
}, [Contents])
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
AdaptProvider,
|
|
53
|
+
when,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const Adapt = withStaticProperties(
|
|
58
|
+
function Adapt({ platform, when, children }: AdaptProps) {
|
|
59
|
+
const context = useContext(AdaptParentContext)
|
|
60
|
+
|
|
61
|
+
let enabled = !platform
|
|
62
|
+
if (platform === 'touch') enabled = isTouchable
|
|
63
|
+
if (platform === 'native') enabled = !isWeb
|
|
64
|
+
if (platform === 'web') enabled = isWeb
|
|
65
|
+
|
|
66
|
+
useIsomorphicLayoutEffect(() => {
|
|
67
|
+
if (!enabled) return
|
|
68
|
+
context?.setWhen(when as MediaQueryKey)
|
|
69
|
+
}, [when, context, enabled])
|
|
70
|
+
|
|
71
|
+
if (!enabled) {
|
|
72
|
+
return null
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return children
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
Contents: AdaptContents,
|
|
79
|
+
}
|
|
80
|
+
)
|
package/src/index.tsx
CHANGED
|
@@ -1,75 +1 @@
|
|
|
1
|
-
|
|
2
|
-
MediaQueryKey,
|
|
3
|
-
isTouchable,
|
|
4
|
-
isWeb,
|
|
5
|
-
useIsomorphicLayoutEffect,
|
|
6
|
-
withStaticProperties,
|
|
7
|
-
} from '@tamagui/core'
|
|
8
|
-
import { createContext, createElement, useContext, useMemo, useState } from 'react'
|
|
9
|
-
|
|
10
|
-
export type AdaptProps = {
|
|
11
|
-
when?: MediaQueryKey
|
|
12
|
-
platform?: 'native' | 'web' | 'touch'
|
|
13
|
-
children?: any
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
type Component = (props: any) => any
|
|
17
|
-
type AdaptParentContextI = {
|
|
18
|
-
Contents: Component
|
|
19
|
-
setWhen: (when: MediaQueryKey) => any
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const AdaptParentContext = createContext<AdaptParentContextI | null>(null)
|
|
23
|
-
|
|
24
|
-
export const AdaptContents = () => {
|
|
25
|
-
const context = useContext(AdaptParentContext)
|
|
26
|
-
if (!context || !context.Contents) {
|
|
27
|
-
throw new Error(`Missing parent adapatable component, Adapt not supported here`)
|
|
28
|
-
}
|
|
29
|
-
return createElement(context.Contents)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const useAdaptParent = ({ Contents }: { Contents: AdaptParentContextI['Contents'] }) => {
|
|
33
|
-
const [when, setWhen] = useState<MediaQueryKey | null>(null)
|
|
34
|
-
|
|
35
|
-
const AdaptProvider = useMemo(() => {
|
|
36
|
-
const context: AdaptParentContextI = {
|
|
37
|
-
Contents,
|
|
38
|
-
setWhen,
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return (props: { children?: any }) => (
|
|
42
|
-
<AdaptParentContext.Provider value={context}>{props.children}</AdaptParentContext.Provider>
|
|
43
|
-
)
|
|
44
|
-
}, [Contents])
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
AdaptProvider,
|
|
48
|
-
when,
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export const Adapt = withStaticProperties(
|
|
53
|
-
function Adapt({ platform, when, children }: AdaptProps) {
|
|
54
|
-
const context = useContext(AdaptParentContext)
|
|
55
|
-
|
|
56
|
-
let enabled = !platform
|
|
57
|
-
if (platform === 'touch') enabled = isTouchable
|
|
58
|
-
if (platform === 'native') enabled = !isWeb
|
|
59
|
-
if (platform === 'web') enabled = isWeb
|
|
60
|
-
|
|
61
|
-
useIsomorphicLayoutEffect(() => {
|
|
62
|
-
if (!enabled) return
|
|
63
|
-
context?.setWhen(when as MediaQueryKey)
|
|
64
|
-
}, [when, context, enabled])
|
|
65
|
-
|
|
66
|
-
if (!enabled) {
|
|
67
|
-
return null
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return children
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
Contents: AdaptContents,
|
|
74
|
-
}
|
|
75
|
-
)
|
|
1
|
+
export * from './Adapt'
|
package/types/Adapt.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { MediaQueryKey } from '@tamagui/core';
|
|
3
|
+
export type AdaptProps = {
|
|
4
|
+
when?: MediaQueryKey;
|
|
5
|
+
platform?: 'native' | 'web' | 'touch';
|
|
6
|
+
children?: any;
|
|
7
|
+
};
|
|
8
|
+
type Component = (props: any) => any;
|
|
9
|
+
type AdaptParentContextI = {
|
|
10
|
+
Contents: Component;
|
|
11
|
+
setWhen: (when: MediaQueryKey) => any;
|
|
12
|
+
};
|
|
13
|
+
export declare const AdaptContents: {
|
|
14
|
+
(props: any): import("react").FunctionComponentElement<any>;
|
|
15
|
+
shouldForwardSpace: boolean;
|
|
16
|
+
};
|
|
17
|
+
export declare const useAdaptParent: ({ Contents }: {
|
|
18
|
+
Contents: AdaptParentContextI['Contents'];
|
|
19
|
+
}) => {
|
|
20
|
+
AdaptProvider: (props: {
|
|
21
|
+
children?: any;
|
|
22
|
+
}) => JSX.Element;
|
|
23
|
+
when: string | number | null;
|
|
24
|
+
};
|
|
25
|
+
export declare const Adapt: (({ platform, when, children }: AdaptProps) => any) & {
|
|
26
|
+
Contents: {
|
|
27
|
+
(props: any): import("react").FunctionComponentElement<any>;
|
|
28
|
+
shouldForwardSpace: boolean;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=Adapt.d.ts.map
|
package/types/index.d.ts
CHANGED
|
@@ -1,26 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
import { MediaQueryKey } from '@tamagui/core';
|
|
3
|
-
export type AdaptProps = {
|
|
4
|
-
when?: MediaQueryKey;
|
|
5
|
-
platform?: 'native' | 'web' | 'touch';
|
|
6
|
-
children?: any;
|
|
7
|
-
};
|
|
8
|
-
type Component = (props: any) => any;
|
|
9
|
-
type AdaptParentContextI = {
|
|
10
|
-
Contents: Component;
|
|
11
|
-
setWhen: (when: MediaQueryKey) => any;
|
|
12
|
-
};
|
|
13
|
-
export declare const AdaptContents: () => import("react").FunctionComponentElement<any>;
|
|
14
|
-
export declare const useAdaptParent: ({ Contents }: {
|
|
15
|
-
Contents: AdaptParentContextI['Contents'];
|
|
16
|
-
}) => {
|
|
17
|
-
AdaptProvider: (props: {
|
|
18
|
-
children?: any;
|
|
19
|
-
}) => JSX.Element;
|
|
20
|
-
when: string | number | null;
|
|
21
|
-
};
|
|
22
|
-
export declare const Adapt: (({ platform, when, children }: AdaptProps) => any) & {
|
|
23
|
-
Contents: () => import("react").FunctionComponentElement<any>;
|
|
24
|
-
};
|
|
25
|
-
export {};
|
|
1
|
+
export * from './Adapt';
|
|
26
2
|
//# sourceMappingURL=index.d.ts.map
|