@hanzogui/use-window-dimensions 2.0.0
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/LICENSE +21 -0
- package/dist/cjs/helpers.cjs +61 -0
- package/dist/cjs/helpers.native.js +47 -0
- package/dist/cjs/helpers.native.js.map +1 -0
- package/dist/cjs/index.cjs +47 -0
- package/dist/cjs/index.native.js +53 -0
- package/dist/cjs/index.native.js.map +1 -0
- package/dist/cjs/initialValue.cjs +35 -0
- package/dist/cjs/initialValue.native.js +38 -0
- package/dist/cjs/initialValue.native.js.map +1 -0
- package/dist/cjs/types.cjs +16 -0
- package/dist/cjs/types.native.js +19 -0
- package/dist/cjs/types.native.js.map +1 -0
- package/dist/esm/helpers.mjs +37 -0
- package/dist/esm/helpers.mjs.map +1 -0
- package/dist/esm/helpers.native.js +20 -0
- package/dist/esm/helpers.native.js.map +1 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/index.mjs +12 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/index.native.js +15 -0
- package/dist/esm/index.native.js.map +1 -0
- package/dist/esm/initialValue.mjs +11 -0
- package/dist/esm/initialValue.mjs.map +1 -0
- package/dist/esm/initialValue.native.js +11 -0
- package/dist/esm/initialValue.native.js.map +1 -0
- package/dist/esm/types.mjs +2 -0
- package/dist/esm/types.mjs.map +1 -0
- package/dist/esm/types.native.js +2 -0
- package/dist/esm/types.native.js.map +1 -0
- package/package.json +48 -0
- package/src/helpers.native.ts +17 -0
- package/src/helpers.ts +68 -0
- package/src/index.ts +16 -0
- package/src/initialValue.ts +15 -0
- package/src/types.ts +10 -0
- package/types/helpers.d.ts +5 -0
- package/types/helpers.d.ts.map +11 -0
- package/types/helpers.native.d.ts +5 -0
- package/types/helpers.native.d.ts.map +11 -0
- package/types/index.d.ts +7 -0
- package/types/index.d.ts.map +11 -0
- package/types/initialValue.d.ts +8 -0
- package/types/initialValue.d.ts.map +11 -0
- package/types/types.d.ts +9 -0
- package/types/types.d.ts.map +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Nate Wienert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: !0
|
|
9
|
+
});
|
|
10
|
+
},
|
|
11
|
+
__copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
13
|
+
get: () => from[key],
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
19
|
+
value: !0
|
|
20
|
+
}), mod);
|
|
21
|
+
var helpers_exports = {};
|
|
22
|
+
__export(helpers_exports, {
|
|
23
|
+
getWindowSize: () => getWindowSize,
|
|
24
|
+
subscribe: () => subscribe
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(helpers_exports);
|
|
27
|
+
var import_constants = require("@hanzogui/constants"),
|
|
28
|
+
import_initialValue = require("./initialValue.cjs");
|
|
29
|
+
let lastSize = import_initialValue.initialValue,
|
|
30
|
+
docEl = null;
|
|
31
|
+
function getWindowSize() {
|
|
32
|
+
docEl ||= window.document.documentElement;
|
|
33
|
+
const nextSize = {
|
|
34
|
+
fontScale: 1,
|
|
35
|
+
height: docEl.clientHeight,
|
|
36
|
+
scale: window.devicePixelRatio || 1,
|
|
37
|
+
width: docEl.clientWidth
|
|
38
|
+
};
|
|
39
|
+
return nextSize.height !== lastSize.height || nextSize.width !== lastSize.width || nextSize.scale !== lastSize.scale ? (lastSize = nextSize, nextSize) : lastSize;
|
|
40
|
+
}
|
|
41
|
+
const cbs = /* @__PURE__ */new Set();
|
|
42
|
+
if (import_constants.isClient) {
|
|
43
|
+
let flushUpdate = function () {
|
|
44
|
+
lastUpdate = Date.now(), cbs.forEach(cb => cb(getWindowSize()));
|
|
45
|
+
},
|
|
46
|
+
lastUpdate = Date.now(),
|
|
47
|
+
tm;
|
|
48
|
+
const USER_MAX_MS = process.env.HANZO_GUI_USE_WINDOW_DIMENSIONS_MAX_UPDATE_MS,
|
|
49
|
+
updateMaxMs = USER_MAX_MS ? +USER_MAX_MS : 100,
|
|
50
|
+
onResize = () => {
|
|
51
|
+
clearTimeout(tm);
|
|
52
|
+
const timeSinceLast = Date.now() - lastUpdate;
|
|
53
|
+
timeSinceLast < updateMaxMs ? tm = setTimeout(() => {
|
|
54
|
+
flushUpdate();
|
|
55
|
+
}, updateMaxMs - timeSinceLast) : flushUpdate();
|
|
56
|
+
};
|
|
57
|
+
window.addEventListener("resize", onResize);
|
|
58
|
+
}
|
|
59
|
+
function subscribe(cb) {
|
|
60
|
+
return cbs.add(cb), () => cbs.delete(cb);
|
|
61
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: !0
|
|
11
|
+
});
|
|
12
|
+
},
|
|
13
|
+
__copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
15
|
+
get: () => from[key],
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
21
|
+
value: !0
|
|
22
|
+
}), mod);
|
|
23
|
+
var helpers_native_exports = {};
|
|
24
|
+
__export(helpers_native_exports, {
|
|
25
|
+
getWindowSize: () => getWindowSize,
|
|
26
|
+
subscribe: () => subscribe
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(helpers_native_exports);
|
|
29
|
+
var import_react_native = require("react-native");
|
|
30
|
+
function getWindowSize() {
|
|
31
|
+
return import_react_native.Dimensions.get("window");
|
|
32
|
+
}
|
|
33
|
+
var cbs = /* @__PURE__ */new Set();
|
|
34
|
+
import_react_native.Dimensions.addEventListener("change", function (param) {
|
|
35
|
+
var {
|
|
36
|
+
window
|
|
37
|
+
} = param;
|
|
38
|
+
cbs.forEach(function (cb) {
|
|
39
|
+
return cb(window);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
function subscribe(cb) {
|
|
43
|
+
return cbs.add(cb), function () {
|
|
44
|
+
return cbs.delete(cb);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=helpers.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["helpers_native_exports","__export","getWindowSize","subscribe","module","exports","__toCommonJS","import_react_native","require","Dimensions","get","cbs","Set","addEventListener","param","window","forEach","cb","add","delete"],"sources":["../../src/helpers.native.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,sBAAA;AAAAC,QAAA,CAAAD,sBAAA;EAAAE,aAAA,EAAAA,CAAA,KAAAA,aAAA;EAAAC,SAAA,EAAAA,CAAA,KAAAA;AAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAC,YAAA,CAAAN,sBAAA;AAAA,IAAAO,mBAAA,GAA2BC,OAAA;AACpB,SAASN,cAAA,EAAgB;EAC5B,OAAOK,mBAAA,CAAAE,UAAA,CAAWC,GAAA,CAAI,QAAQ;AAClC;AACA,IAAIC,GAAA,GAAM,mBAAIC,GAAA,CAAI;AAClBL,mBAAA,CAAAE,UAAA,CAAWI,gBAAA,CAAiB,UAAU,UAASC,KAAA,EAAO;EAClD,IAAI;IAAEC;EAAO,IAAID,KAAA;EACjBH,GAAA,CAAIK,OAAA,CAAQ,UAASC,EAAA,EAAI;IACrB,OAAOA,EAAA,CAAGF,MAAM;EACpB,CAAC;AACL,CAAC;AACM,SAASZ,UAAUc,EAAA,EAAI;EAC1B,OAAAN,GAAA,CAAIO,GAAA,CAAID,EAAE,GACH,YAAW;IACd,OAAON,GAAA,CAAIQ,MAAA,CAAOF,EAAE;EACxB;AACJ","ignoreList":[]}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf,
|
|
6
|
+
__hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: !0
|
|
11
|
+
});
|
|
12
|
+
},
|
|
13
|
+
__copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
15
|
+
get: () => from[key],
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
26
|
+
value: mod,
|
|
27
|
+
enumerable: !0
|
|
28
|
+
}) : target, mod)),
|
|
29
|
+
__toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
30
|
+
value: !0
|
|
31
|
+
}), mod);
|
|
32
|
+
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
configureInitialWindowDimensions: () => import_initialValue2.configureInitialWindowDimensions,
|
|
35
|
+
useWindowDimensions: () => useWindowDimensions
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(index_exports);
|
|
38
|
+
var import_react = __toESM(require("react"), 1),
|
|
39
|
+
import_constants = require("@hanzogui/constants"),
|
|
40
|
+
import_helpers = require("./helpers.cjs"),
|
|
41
|
+
import_initialValue = require("./initialValue.cjs"),
|
|
42
|
+
import_initialValue2 = require("./initialValue.cjs");
|
|
43
|
+
function useWindowDimensions({
|
|
44
|
+
serverValue = import_initialValue.initialValue
|
|
45
|
+
} = {}) {
|
|
46
|
+
return import_react.default.useSyncExternalStore(import_helpers.subscribe, import_helpers.getWindowSize, () => import_constants.isWeb ? serverValue : (0, import_helpers.getWindowSize)());
|
|
47
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf,
|
|
8
|
+
__hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all) __defProp(target, name, {
|
|
11
|
+
get: all[name],
|
|
12
|
+
enumerable: !0
|
|
13
|
+
});
|
|
14
|
+
},
|
|
15
|
+
__copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
17
|
+
get: () => from[key],
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
+
value: mod,
|
|
29
|
+
enumerable: !0
|
|
30
|
+
}) : target, mod)),
|
|
31
|
+
__toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
32
|
+
value: !0
|
|
33
|
+
}), mod);
|
|
34
|
+
var index_exports = {};
|
|
35
|
+
__export(index_exports, {
|
|
36
|
+
configureInitialWindowDimensions: () => import_initialValue2.configureInitialWindowDimensions,
|
|
37
|
+
useWindowDimensions: () => useWindowDimensions
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
var import_react = __toESM(require("react"), 1),
|
|
41
|
+
import_constants = require("@hanzogui/constants"),
|
|
42
|
+
import_helpers = require("./helpers.native.js"),
|
|
43
|
+
import_initialValue = require("./initialValue.native.js"),
|
|
44
|
+
import_initialValue2 = require("./initialValue.native.js");
|
|
45
|
+
function useWindowDimensions() {
|
|
46
|
+
var {
|
|
47
|
+
serverValue = import_initialValue.initialValue
|
|
48
|
+
} = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
49
|
+
return import_react.default.useSyncExternalStore(import_helpers.subscribe, import_helpers.getWindowSize, function () {
|
|
50
|
+
return import_constants.isWeb ? serverValue : (0, import_helpers.getWindowSize)();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=index.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","index_exports","__export","configureInitialWindowDimensions","import_initialValue2","useWindowDimensions","module","exports","import_react","__toESM","require","import_constants","import_helpers","import_initialValue","serverValue","initialValue","arguments","length","default","useSyncExternalStore","subscribe","getWindowSize","isWeb"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAAA;EAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;IAAAC,KAAA;EAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA;AAAAC,QAAA,CAAAD,aAAA;EAAAE,gCAAA,EAAAA,CAAA,KAAAC,oBAAA,CAAAD,gCAAA;EAAAE,mBAAA,EAAAA,CAAA,KAAAA;AAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAX,YAAkB,CAAAK,aAAA;AASX,IAAAO,YAAS,GAAAC,OAAA,CAAAC,OAAoB;EAAAC,gBAAA,GAAAD,OAAA;EAAAE,cAAA,GAAAF,OAAA;EAAAG,mBAAA,GAAAH,OAAA;EAAAN,oBAAA,GAAAM,OAAA;AAAA,SAClCL,mBAAcA,CAAA;EAChB,IAAmC;IAAeS,WAAA,GAAAD,mBAAA,CAAAE;EAAA,IAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,iBAAAA,SAAA;EAChD,OAAOR,YAAA,CAAAU,OAAA,CAAMC,oBAAA,CAAAP,cAAA,CAAAQ,SAAA,EAAAR,cAAA,CAAAS,aAAA;IAAqB,OAAAV,gBAAA,CAAAW,KAAA,GAAAR,WAAA,OAAAF,cAAA,CAAAS,aAAA;EAAA,EAAW;AAAA","ignoreList":[]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: !0
|
|
9
|
+
});
|
|
10
|
+
},
|
|
11
|
+
__copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
13
|
+
get: () => from[key],
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
19
|
+
value: !0
|
|
20
|
+
}), mod);
|
|
21
|
+
var initialValue_exports = {};
|
|
22
|
+
__export(initialValue_exports, {
|
|
23
|
+
configureInitialWindowDimensions: () => configureInitialWindowDimensions,
|
|
24
|
+
initialValue: () => initialValue
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(initialValue_exports);
|
|
27
|
+
const initialValue = {
|
|
28
|
+
width: 800,
|
|
29
|
+
height: 600,
|
|
30
|
+
scale: 1,
|
|
31
|
+
fontScale: 1
|
|
32
|
+
};
|
|
33
|
+
function configureInitialWindowDimensions(next) {
|
|
34
|
+
Object.assign(initialValue, next);
|
|
35
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: !0
|
|
11
|
+
});
|
|
12
|
+
},
|
|
13
|
+
__copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
15
|
+
get: () => from[key],
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
21
|
+
value: !0
|
|
22
|
+
}), mod);
|
|
23
|
+
var initialValue_exports = {};
|
|
24
|
+
__export(initialValue_exports, {
|
|
25
|
+
configureInitialWindowDimensions: () => configureInitialWindowDimensions,
|
|
26
|
+
initialValue: () => initialValue
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(initialValue_exports);
|
|
29
|
+
var initialValue = {
|
|
30
|
+
width: 800,
|
|
31
|
+
height: 600,
|
|
32
|
+
scale: 1,
|
|
33
|
+
fontScale: 1
|
|
34
|
+
};
|
|
35
|
+
function configureInitialWindowDimensions(next) {
|
|
36
|
+
Object.assign(initialValue, next);
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=initialValue.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","initialValue_exports","__export","configureInitialWindowDimensions","initialValue","module","exports","width","height","scale","fontScale","next","Object","assign"],"sources":["../../src/initialValue.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,oBAAA;AAAAC,QAAA,CAAAD,oBAAA;EAAAE,gCAAA,EAAAA,CAAA,KAAAA,gCAAA;EAAAC,YAAA,EAAAA,CAAA,KAAAA;AAAA;AAKOC,MAAM,CAAAC,OAAA,GAAAV,YAA2B,CAAAK,oBAAA;AAAA,IACtCG,YAAO;EACPG,KAAA,KAAQ;EACRC,MAAA,EAAO;EACPC,KAAA;EACFC,SAAA;AAEO;AACL,SAAOP,gCAAyBA,CAAAQ,IAAA;EAClCC,MAAA,CAAAC,MAAA,CAAAT,YAAA,EAAAO,IAAA","ignoreList":[]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
7
|
+
get: () => from[key],
|
|
8
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
9
|
+
});
|
|
10
|
+
return to;
|
|
11
|
+
};
|
|
12
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
13
|
+
value: !0
|
|
14
|
+
}), mod);
|
|
15
|
+
var types_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __copyProps = (to, from, except, desc) => {
|
|
8
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
9
|
+
get: () => from[key],
|
|
10
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
11
|
+
});
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
15
|
+
value: !0
|
|
16
|
+
}), mod);
|
|
17
|
+
var types_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(types_exports);
|
|
19
|
+
//# sourceMappingURL=types.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","types_exports"],"sources":["../../src/types.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA","ignoreList":[]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { isClient } from "@hanzogui/constants";
|
|
2
|
+
import { initialValue } from "./initialValue.mjs";
|
|
3
|
+
let lastSize = initialValue,
|
|
4
|
+
docEl = null;
|
|
5
|
+
function getWindowSize() {
|
|
6
|
+
docEl ||= window.document.documentElement;
|
|
7
|
+
const nextSize = {
|
|
8
|
+
fontScale: 1,
|
|
9
|
+
height: docEl.clientHeight,
|
|
10
|
+
scale: window.devicePixelRatio || 1,
|
|
11
|
+
width: docEl.clientWidth
|
|
12
|
+
};
|
|
13
|
+
return nextSize.height !== lastSize.height || nextSize.width !== lastSize.width || nextSize.scale !== lastSize.scale ? (lastSize = nextSize, nextSize) : lastSize;
|
|
14
|
+
}
|
|
15
|
+
const cbs = /* @__PURE__ */new Set();
|
|
16
|
+
if (isClient) {
|
|
17
|
+
let flushUpdate = function () {
|
|
18
|
+
lastUpdate = Date.now(), cbs.forEach(cb => cb(getWindowSize()));
|
|
19
|
+
},
|
|
20
|
+
lastUpdate = Date.now(),
|
|
21
|
+
tm;
|
|
22
|
+
const USER_MAX_MS = process.env.HANZO_GUI_USE_WINDOW_DIMENSIONS_MAX_UPDATE_MS,
|
|
23
|
+
updateMaxMs = USER_MAX_MS ? +USER_MAX_MS : 100,
|
|
24
|
+
onResize = () => {
|
|
25
|
+
clearTimeout(tm);
|
|
26
|
+
const timeSinceLast = Date.now() - lastUpdate;
|
|
27
|
+
timeSinceLast < updateMaxMs ? tm = setTimeout(() => {
|
|
28
|
+
flushUpdate();
|
|
29
|
+
}, updateMaxMs - timeSinceLast) : flushUpdate();
|
|
30
|
+
};
|
|
31
|
+
window.addEventListener("resize", onResize);
|
|
32
|
+
}
|
|
33
|
+
function subscribe(cb) {
|
|
34
|
+
return cbs.add(cb), () => cbs.delete(cb);
|
|
35
|
+
}
|
|
36
|
+
export { getWindowSize, subscribe };
|
|
37
|
+
//# sourceMappingURL=helpers.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["isClient","initialValue","lastSize","docEl","getWindowSize","window","document","documentElement","nextSize","fontScale","height","clientHeight","scale","devicePixelRatio","width","clientWidth","cbs","Set","flushUpdate","lastUpdate","Date","now","forEach","cb","tm","USER_MAX_MS","process","env","HANZO_GUI_USE_WINDOW_DIMENSIONS_MAX_UPDATE_MS","updateMaxMs","onResize","clearTimeout","timeSinceLast","setTimeout","addEventListener","subscribe","add","delete"],"sources":["../../src/helpers.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,QAAA,QAAgB;AACzB,SAASC,YAAA,QAAoB;AAG7B,IAAIC,QAAA,GAAuBD,YAAA;EACvBE,KAAA,GAA4B;AAEzB,SAASC,cAAA,EAA4B;EAK1CD,KAAA,KAAUE,MAAA,CAAOC,QAAA,CAASC,eAAA;EAE1B,MAAMC,QAAA,GAAuB;IAC3BC,SAAA,EAAW;IACXC,MAAA,EAAQP,KAAA,CAAMQ,YAAA;IACdC,KAAA,EAAOP,MAAA,CAAOQ,gBAAA,IAAoB;IAClCC,KAAA,EAAOX,KAAA,CAAMY;EACf;EAEA,OACEP,QAAA,CAASE,MAAA,KAAWR,QAAA,CAASQ,MAAA,IAC7BF,QAAA,CAASM,KAAA,KAAUZ,QAAA,CAASY,KAAA,IAC5BN,QAAA,CAASI,KAAA,KAAUV,QAAA,CAASU,KAAA,IAE5BV,QAAA,GAAWM,QAAA,EACJA,QAAA,IAGFN,QAAA;AACT;AAEA,MAAMc,GAAA,GAAM,mBAAIC,GAAA,CAAwB;AAGxC,IAAIjB,QAAA,EAAU;EAMZ,IAASkB,WAAA,GAAT,SAAAA,CAAA,EAAuB;MACrBC,UAAA,GAAaC,IAAA,CAAKC,GAAA,CAAI,GACtBL,GAAA,CAAIM,OAAA,CAASC,EAAA,IAAOA,EAAA,CAAGnB,aAAA,CAAc,CAAC,CAAC;IACzC;IARIe,UAAA,GAAaC,IAAA,CAAKC,GAAA,CAAI;IACtBG,EAAA;EACJ,MAAMC,WAAA,GAAcC,OAAA,CAAQC,GAAA,CAAIC,6CAAA;IAC1BC,WAAA,GAAcJ,WAAA,GAAc,CAACA,WAAA,GAAc;IAO3CK,QAAA,GAAWA,CAAA,KAAM;MACrBC,YAAA,CAAaP,EAAE;MAGf,MAAMQ,aAAA,GAAgBZ,IAAA,CAAKC,GAAA,CAAI,IAAIF,UAAA;MAC/Ba,aAAA,GAAgBH,WAAA,GAClBL,EAAA,GAAKS,UAAA,CAAW,MAAM;QACpBf,WAAA,CAAY;MACd,GAAGW,WAAA,GAAcG,aAAa,IAE9Bd,WAAA,CAAY;IAEhB;EAEAb,MAAA,CAAO6B,gBAAA,CAAiB,UAAUJ,QAAQ;AAC5C;AAEO,SAASK,UAAUZ,EAAA,EAAoC;EAC5D,OAAAP,GAAA,CAAIoB,GAAA,CAAIb,EAAE,GACH,MAAMP,GAAA,CAAIqB,MAAA,CAAOd,EAAE;AAC5B","ignoreList":[]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Dimensions } from "react-native";
|
|
2
|
+
function getWindowSize() {
|
|
3
|
+
return Dimensions.get("window");
|
|
4
|
+
}
|
|
5
|
+
var cbs = /* @__PURE__ */new Set();
|
|
6
|
+
Dimensions.addEventListener("change", function (param) {
|
|
7
|
+
var {
|
|
8
|
+
window
|
|
9
|
+
} = param;
|
|
10
|
+
cbs.forEach(function (cb) {
|
|
11
|
+
return cb(window);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
function subscribe(cb) {
|
|
15
|
+
return cbs.add(cb), function () {
|
|
16
|
+
return cbs.delete(cb);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export { getWindowSize, subscribe };
|
|
20
|
+
//# sourceMappingURL=helpers.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["Dimensions","getWindowSize","get","cbs","Set","addEventListener","param","window","forEach","cb","subscribe","add","delete"],"sources":["../../src/helpers.native.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,UAAA,QAAkB;AACpB,SAASC,cAAA,EAAgB;EAC5B,OAAOD,UAAA,CAAWE,GAAA,CAAI,QAAQ;AAClC;AACA,IAAIC,GAAA,GAAM,mBAAIC,GAAA,CAAI;AAClBJ,UAAA,CAAWK,gBAAA,CAAiB,UAAU,UAASC,KAAA,EAAO;EAClD,IAAI;IAAEC;EAAO,IAAID,KAAA;EACjBH,GAAA,CAAIK,OAAA,CAAQ,UAASC,EAAA,EAAI;IACrB,OAAOA,EAAA,CAAGF,MAAM;EACpB,CAAC;AACL,CAAC;AACM,SAASG,UAAUD,EAAA,EAAI;EAC1B,OAAAN,GAAA,CAAIQ,GAAA,CAAIF,EAAE,GACH,YAAW;IACd,OAAON,GAAA,CAAIS,MAAA,CAAOH,EAAE;EACxB;AACJ","ignoreList":[]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { isWeb } from "@hanzogui/constants";
|
|
3
|
+
import { getWindowSize, subscribe } from "./helpers.mjs";
|
|
4
|
+
import { initialValue } from "./initialValue.mjs";
|
|
5
|
+
import { configureInitialWindowDimensions } from "./initialValue.mjs";
|
|
6
|
+
function useWindowDimensions({
|
|
7
|
+
serverValue = initialValue
|
|
8
|
+
} = {}) {
|
|
9
|
+
return React.useSyncExternalStore(subscribe, getWindowSize, () => isWeb ? serverValue : getWindowSize());
|
|
10
|
+
}
|
|
11
|
+
export { configureInitialWindowDimensions, useWindowDimensions };
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","isWeb","getWindowSize","subscribe","initialValue","configureInitialWindowDimensions","useWindowDimensions","serverValue","useSyncExternalStore"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,OAAOA,KAAA,MAAW;AAClB,SAASC,KAAA,QAAa;AAEtB,SAASC,aAAA,EAAeC,SAAA,QAAiB;AAEzC,SAASC,YAAA,QAAoB;AAE7B,SAASC,gCAAA,QAAwC;AAE1C,SAASC,oBAAoB;EAClCC,WAAA,GAAcH;AAChB,IAAkC,CAAC,GAAe;EAChD,OAAOJ,KAAA,CAAMQ,oBAAA,CAAqBL,SAAA,EAAWD,aAAA,EAAe,MAC1DD,KAAA,GAAQM,WAAA,GAAcL,aAAA,CAAc,CACtC;AACF","ignoreList":[]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { isWeb } from "@hanzogui/constants";
|
|
3
|
+
import { getWindowSize, subscribe } from "./helpers.mjs";
|
|
4
|
+
import { initialValue } from "./initialValue.mjs";
|
|
5
|
+
import { configureInitialWindowDimensions } from "./initialValue.mjs";
|
|
6
|
+
function useWindowDimensions({
|
|
7
|
+
serverValue = initialValue
|
|
8
|
+
} = {}) {
|
|
9
|
+
return React.useSyncExternalStore(subscribe, getWindowSize, () => isWeb ? serverValue : getWindowSize());
|
|
10
|
+
}
|
|
11
|
+
export { configureInitialWindowDimensions, useWindowDimensions };
|
|
12
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","isWeb","getWindowSize","subscribe","initialValue","configureInitialWindowDimensions","useWindowDimensions","serverValue","useSyncExternalStore"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,OAAOA,KAAA,MAAW;AAClB,SAASC,KAAA,QAAa;AAEtB,SAASC,aAAA,EAAeC,SAAA,QAAiB;AAEzC,SAASC,YAAA,QAAoB;AAE7B,SAASC,gCAAA,QAAwC;AAE1C,SAASC,oBAAoB;EAClCC,WAAA,GAAcH;AAChB,IAAkC,CAAC,GAAe;EAChD,OAAOJ,KAAA,CAAMQ,oBAAA,CAAqBL,SAAA,EAAWD,aAAA,EAAe,MAC1DD,KAAA,GAAQM,WAAA,GAAcL,aAAA,CAAc,CACtC;AACF","ignoreList":[]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { isWeb } from "@hanzogui/constants";
|
|
3
|
+
import { getWindowSize, subscribe } from "./helpers.native.js";
|
|
4
|
+
import { initialValue } from "./initialValue.native.js";
|
|
5
|
+
import { configureInitialWindowDimensions } from "./initialValue.native.js";
|
|
6
|
+
function useWindowDimensions() {
|
|
7
|
+
var {
|
|
8
|
+
serverValue = initialValue
|
|
9
|
+
} = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
10
|
+
return React.useSyncExternalStore(subscribe, getWindowSize, function () {
|
|
11
|
+
return isWeb ? serverValue : getWindowSize();
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export { configureInitialWindowDimensions, useWindowDimensions };
|
|
15
|
+
//# sourceMappingURL=index.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","isWeb","getWindowSize","subscribe","initialValue","configureInitialWindowDimensions","useWindowDimensions","serverValue","arguments","length","useSyncExternalStore"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,OAAOA,KAAA,MAAW;AAClB,SAASC,KAAA,QAAa;AAEtB,SAASC,aAAA,EAAeC,SAAA,QAAiB;AAEzC,SAASC,YAAA,QAAoB;AAE7B,SAASC,gCAAA,QAAwC;AAE1C,SAASC,oBAAA,EAAoB;EAClC;IAAAC,WAAc,GAAAH;EAAA,IAAAI,SAAA,CAAAC,MAAA,QAAAD,SAAA,iBAAAA,SAAA;EAChB,OAAkDR,KAAA,CAAAU,oBAAA,CAAAP,SAAA,EAAAD,aAAA;IAChD,OAAOD,KAAM,GAAAM,WAAA,GAAAL,aAAA;EAAA,EAAqB;AAAA;AAAW,SAE7CG,gCAAA,EACFC,mBAAA","ignoreList":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const initialValue = {
|
|
2
|
+
width: 800,
|
|
3
|
+
height: 600,
|
|
4
|
+
scale: 1,
|
|
5
|
+
fontScale: 1
|
|
6
|
+
};
|
|
7
|
+
function configureInitialWindowDimensions(next) {
|
|
8
|
+
Object.assign(initialValue, next);
|
|
9
|
+
}
|
|
10
|
+
export { configureInitialWindowDimensions, initialValue };
|
|
11
|
+
//# sourceMappingURL=initialValue.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["initialValue","width","height","scale","fontScale","configureInitialWindowDimensions","next","Object","assign"],"sources":["../../src/initialValue.ts"],"sourcesContent":[null],"mappings":"AAKO,MAAMA,YAAA,GAA2B;EACtCC,KAAA,EAAO;EACPC,MAAA,EAAQ;EACRC,KAAA,EAAO;EACPC,SAAA,EAAW;AACb;AAEO,SAASC,iCAAiCC,IAAA,EAAwB;EACvEC,MAAA,CAAOC,MAAA,CAAOR,YAAA,EAAcM,IAAI;AAClC","ignoreList":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
var initialValue = {
|
|
2
|
+
width: 800,
|
|
3
|
+
height: 600,
|
|
4
|
+
scale: 1,
|
|
5
|
+
fontScale: 1
|
|
6
|
+
};
|
|
7
|
+
function configureInitialWindowDimensions(next) {
|
|
8
|
+
Object.assign(initialValue, next);
|
|
9
|
+
}
|
|
10
|
+
export { configureInitialWindowDimensions, initialValue };
|
|
11
|
+
//# sourceMappingURL=initialValue.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["initialValue","width","height","scale","fontScale","configureInitialWindowDimensions","next","Object","assign"],"sources":["../../src/initialValue.ts"],"sourcesContent":[null],"mappings":"AAKO,IAAAA,YAAM;EACXC,KAAA,EAAO;EACPC,MAAA,EAAQ;EACRC,KAAA,EAAO;EACPC,SAAA,EAAW;AACb;AAEO,SAASC,iCAAiCC,IAAA,EAAwB;EACvEC,MAAA,CAAOC,MAAA,CAAOR,YAAA,EAAcM,IAAI;AAClC","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sources":[],"sourcesContent":[],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sources":[],"sourcesContent":[],"mappings":"","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hanzogui/use-window-dimensions",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"gitHead": "a49cc7ea6b93ba384e77a4880ae48ac4a5635c14",
|
|
5
|
+
"files": [
|
|
6
|
+
"src",
|
|
7
|
+
"types",
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"type": "module",
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"main": "dist/cjs",
|
|
13
|
+
"module": "dist/esm",
|
|
14
|
+
"types": "./types/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
"./package.json": "./package.json",
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./types/index.d.ts",
|
|
19
|
+
"react-native": "./dist/esm/index.native.js",
|
|
20
|
+
"browser": "./dist/esm/index.mjs",
|
|
21
|
+
"module": "./dist/esm/index.mjs",
|
|
22
|
+
"import": "./dist/esm/index.mjs",
|
|
23
|
+
"require": "./dist/cjs/index.cjs",
|
|
24
|
+
"default": "./dist/esm/index.mjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "hanzo-gui-build",
|
|
32
|
+
"watch": "hanzo-gui-build --watch",
|
|
33
|
+
"clean": "hanzo-gui-build clean",
|
|
34
|
+
"clean:build": "hanzo-gui-build clean:build"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@hanzogui/constants": "workspace:*"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@hanzogui/build": "workspace:*",
|
|
41
|
+
"react": ">=19",
|
|
42
|
+
"react-native": "0.83.2"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"react": ">=19",
|
|
46
|
+
"react-native": "*"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Dimensions } from 'react-native'
|
|
2
|
+
import type { WindowSize, WindowSizeListener } from './types'
|
|
3
|
+
|
|
4
|
+
export function getWindowSize(): WindowSize {
|
|
5
|
+
return Dimensions.get('window')
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const cbs = new Set<WindowSizeListener>()
|
|
9
|
+
|
|
10
|
+
Dimensions.addEventListener('change', ({ window }) => {
|
|
11
|
+
cbs.forEach((cb) => cb(window))
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
export function subscribe(cb: WindowSizeListener): () => void {
|
|
15
|
+
cbs.add(cb)
|
|
16
|
+
return () => cbs.delete(cb)
|
|
17
|
+
}
|
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { isClient } from '@hanzogui/constants'
|
|
2
|
+
import { initialValue } from './initialValue'
|
|
3
|
+
import type { WindowSize, WindowSizeListener } from './types'
|
|
4
|
+
|
|
5
|
+
let lastSize: WindowSize = initialValue
|
|
6
|
+
let docEl: HTMLElement | null = null
|
|
7
|
+
|
|
8
|
+
export function getWindowSize(): WindowSize {
|
|
9
|
+
if (process.env.HANZO_GUI_TARGET !== 'web') {
|
|
10
|
+
return initialValue
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
docEl ||= window.document.documentElement
|
|
14
|
+
|
|
15
|
+
const nextSize: WindowSize = {
|
|
16
|
+
fontScale: 1,
|
|
17
|
+
height: docEl.clientHeight,
|
|
18
|
+
scale: window.devicePixelRatio || 1,
|
|
19
|
+
width: docEl.clientWidth,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (
|
|
23
|
+
nextSize.height !== lastSize.height ||
|
|
24
|
+
nextSize.width !== lastSize.width ||
|
|
25
|
+
nextSize.scale !== lastSize.scale
|
|
26
|
+
) {
|
|
27
|
+
lastSize = nextSize
|
|
28
|
+
return nextSize
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return lastSize
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const cbs = new Set<WindowSizeListener>()
|
|
35
|
+
|
|
36
|
+
// only attach resize listener on client (not during SSR)
|
|
37
|
+
if (isClient) {
|
|
38
|
+
let lastUpdate = Date.now()
|
|
39
|
+
let tm: ReturnType<typeof setTimeout> | undefined
|
|
40
|
+
const USER_MAX_MS = process.env.HANZO_GUI_USE_WINDOW_DIMENSIONS_MAX_UPDATE_MS
|
|
41
|
+
const updateMaxMs = USER_MAX_MS ? +USER_MAX_MS : 100
|
|
42
|
+
|
|
43
|
+
function flushUpdate() {
|
|
44
|
+
lastUpdate = Date.now()
|
|
45
|
+
cbs.forEach((cb) => cb(getWindowSize()))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const onResize = () => {
|
|
49
|
+
clearTimeout(tm)
|
|
50
|
+
|
|
51
|
+
// only update every few frames
|
|
52
|
+
const timeSinceLast = Date.now() - lastUpdate
|
|
53
|
+
if (timeSinceLast < updateMaxMs) {
|
|
54
|
+
tm = setTimeout(() => {
|
|
55
|
+
flushUpdate()
|
|
56
|
+
}, updateMaxMs - timeSinceLast)
|
|
57
|
+
} else {
|
|
58
|
+
flushUpdate()
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
window.addEventListener('resize', onResize)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function subscribe(cb: WindowSizeListener): () => void {
|
|
66
|
+
cbs.add(cb)
|
|
67
|
+
return () => cbs.delete(cb)
|
|
68
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { isWeb } from '@hanzogui/constants'
|
|
3
|
+
|
|
4
|
+
import { getWindowSize, subscribe } from './helpers'
|
|
5
|
+
import type { WindowSize } from './types'
|
|
6
|
+
import { initialValue } from './initialValue'
|
|
7
|
+
|
|
8
|
+
export { configureInitialWindowDimensions } from './initialValue'
|
|
9
|
+
|
|
10
|
+
export function useWindowDimensions({
|
|
11
|
+
serverValue = initialValue,
|
|
12
|
+
}: { serverValue?: WindowSize } = {}): WindowSize {
|
|
13
|
+
return React.useSyncExternalStore(subscribe, getWindowSize, () =>
|
|
14
|
+
isWeb ? serverValue : getWindowSize()
|
|
15
|
+
)
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { WindowSize } from './types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SSR safe useWindowDimensions
|
|
5
|
+
*/
|
|
6
|
+
export const initialValue: WindowSize = {
|
|
7
|
+
width: 800,
|
|
8
|
+
height: 600,
|
|
9
|
+
scale: 1,
|
|
10
|
+
fontScale: 1,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function configureInitialWindowDimensions(next: WindowSize): void {
|
|
14
|
+
Object.assign(initialValue, next)
|
|
15
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AAEA,cAAc,YAAY,0BAA0B;AAKpD,OAAO,iBAAS,iBAAiB;AAyDjC,OAAO,iBAAS,UAAU,IAAI",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/helpers.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"import { isClient } from '@hanzogui/constants'\nimport { initialValue } from './initialValue'\nimport type { WindowSize, WindowSizeListener } from './types'\n\nlet lastSize: WindowSize = initialValue\nlet docEl: HTMLElement | null = null\n\nexport function getWindowSize(): WindowSize {\n if (process.env.HANZO_GUI_TARGET !== 'web') {\n return initialValue\n }\n\n docEl ||= window.document.documentElement\n\n const nextSize: WindowSize = {\n fontScale: 1,\n height: docEl.clientHeight,\n scale: window.devicePixelRatio || 1,\n width: docEl.clientWidth,\n }\n\n if (\n nextSize.height !== lastSize.height ||\n nextSize.width !== lastSize.width ||\n nextSize.scale !== lastSize.scale\n ) {\n lastSize = nextSize\n return nextSize\n }\n\n return lastSize\n}\n\nconst cbs = new Set<WindowSizeListener>()\n\n// only attach resize listener on client (not during SSR)\nif (isClient) {\n let lastUpdate = Date.now()\n let tm: ReturnType<typeof setTimeout> | undefined\n const USER_MAX_MS = process.env.HANZO_GUI_USE_WINDOW_DIMENSIONS_MAX_UPDATE_MS\n const updateMaxMs = USER_MAX_MS ? +USER_MAX_MS : 100\n\n function flushUpdate() {\n lastUpdate = Date.now()\n cbs.forEach((cb) => cb(getWindowSize()))\n }\n\n const onResize = () => {\n clearTimeout(tm)\n\n // only update every few frames\n const timeSinceLast = Date.now() - lastUpdate\n if (timeSinceLast < updateMaxMs) {\n tm = setTimeout(() => {\n flushUpdate()\n }, updateMaxMs - timeSinceLast)\n } else {\n flushUpdate()\n }\n }\n\n window.addEventListener('resize', onResize)\n}\n\nexport function subscribe(cb: WindowSizeListener): () => void {\n cbs.add(cb)\n return () => cbs.delete(cb)\n}\n"
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AACA,cAAc,YAAY,0BAA0B;AAEpD,OAAO,iBAAS,iBAAiB;AAUjC,OAAO,iBAAS,UAAU,IAAI",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/helpers.native.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"import { Dimensions } from 'react-native'\nimport type { WindowSize, WindowSizeListener } from './types'\n\nexport function getWindowSize(): WindowSize {\n return Dimensions.get('window')\n}\n\nconst cbs = new Set<WindowSizeListener>()\n\nDimensions.addEventListener('change', ({ window }) => {\n cbs.forEach((cb) => cb(window))\n})\n\nexport function subscribe(cb: WindowSizeListener): () => void {\n cbs.add(cb)\n return () => cbs.delete(cb)\n}\n"
|
|
10
|
+
]
|
|
11
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AAIA,cAAc,kBAAkB;AAGhC,SAAS,wCAAwC;AAEjD,OAAO,iBAAS,oBAAoB,EAClC,gBACC;CAAE,cAAc;IAAoB",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/index.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"import React from 'react'\nimport { isWeb } from '@hanzogui/constants'\n\nimport { getWindowSize, subscribe } from './helpers'\nimport type { WindowSize } from './types'\nimport { initialValue } from './initialValue'\n\nexport { configureInitialWindowDimensions } from './initialValue'\n\nexport function useWindowDimensions({\n serverValue = initialValue,\n}: { serverValue?: WindowSize } = {}): WindowSize {\n return React.useSyncExternalStore(subscribe, getWindowSize, () =>\n isWeb ? serverValue : getWindowSize()\n )\n}\n"
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { WindowSize } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* SSR safe useWindowDimensions
|
|
4
|
+
*/
|
|
5
|
+
export declare const initialValue: WindowSize;
|
|
6
|
+
export declare function configureInitialWindowDimensions(next: WindowSize): void;
|
|
7
|
+
|
|
8
|
+
//# sourceMappingURL=initialValue.d.ts.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AAAA,cAAc,kBAAkB;;;;AAKhC,OAAO,cAAM,cAAc;AAO3B,OAAO,iBAAS,iCAAiC,MAAM",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/initialValue.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"import type { WindowSize } from './types'\n\n/**\n * SSR safe useWindowDimensions\n */\nexport const initialValue: WindowSize = {\n width: 800,\n height: 600,\n scale: 1,\n fontScale: 1,\n}\n\nexport function configureInitialWindowDimensions(next: WindowSize): void {\n Object.assign(initialValue, next)\n}\n"
|
|
10
|
+
]
|
|
11
|
+
}
|
package/types/types.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AAAA,cAAc,kBAAkB;AAEhC,YAAY,OAAO;CACjB;CACA;;AAGF,YAAY,aAAa;AAEzB,YAAY,sBAAsB,MAAM",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/types.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"import type { ScaledSize } from 'react-native'\n\nexport type Size = {\n width: number\n height: number\n}\n\nexport type WindowSize = ScaledSize\n\nexport type WindowSizeListener = (size: WindowSize) => void\n"
|
|
10
|
+
]
|
|
11
|
+
}
|