vfit 0.1.1 → 0.1.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/fitscale.es.js +70 -43
- package/dist/fitscale.umd.js +1 -1
- package/package.json +3 -2
package/dist/fitscale.es.js
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import { defineComponent
|
|
2
|
-
function
|
|
3
|
-
const
|
|
4
|
-
for (const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { defineComponent, reactive, inject, ref, watch, createElementBlock, openBlock, normalizeStyle, normalizeClass, renderSlot } from "vue";
|
|
2
|
+
function observeScale(target, designHeight, onScale) {
|
|
3
|
+
const observer = new ResizeObserver((entries) => {
|
|
4
|
+
for (const entry of entries) {
|
|
5
|
+
const rectHeight = entry.contentRect.height;
|
|
6
|
+
const rectWidth = entry.contentRect.width;
|
|
7
|
+
let scaleVal;
|
|
8
|
+
if (rectWidth / rectHeight < 1.7666666666666666) {
|
|
9
|
+
scaleVal = rectWidth / 1920 * 0.98;
|
|
10
|
+
} else {
|
|
11
|
+
scaleVal = rectHeight / designHeight;
|
|
12
|
+
}
|
|
13
|
+
onScale(scaleVal);
|
|
8
14
|
}
|
|
9
15
|
});
|
|
10
|
-
|
|
16
|
+
observer.observe(target);
|
|
17
|
+
return observer;
|
|
11
18
|
}
|
|
12
|
-
const
|
|
19
|
+
const _sfc_main = /* @__PURE__ */ defineComponent({
|
|
13
20
|
__name: "FitContainer",
|
|
14
21
|
props: {
|
|
15
22
|
scale: { type: Number, default: 0 },
|
|
@@ -19,50 +26,70 @@ const _ = /* @__PURE__ */ m({
|
|
|
19
26
|
right: { type: Number },
|
|
20
27
|
unit: { type: String, default: "px" }
|
|
21
28
|
},
|
|
22
|
-
setup(
|
|
23
|
-
const
|
|
24
|
-
scale:
|
|
29
|
+
setup(__props) {
|
|
30
|
+
const position = reactive({
|
|
31
|
+
scale: `scale(1)`,
|
|
25
32
|
top: "auto",
|
|
26
33
|
bottom: "auto",
|
|
27
34
|
left: "auto",
|
|
28
35
|
right: "auto"
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
});
|
|
37
|
+
const props = __props;
|
|
38
|
+
const fitScale = inject(FitScaleKey, ref(1));
|
|
39
|
+
watch([() => props.scale, fitScale], () => {
|
|
40
|
+
const s = props.scale && props.scale > 0 ? props.scale : (fitScale == null ? void 0 : fitScale.value) ?? 1;
|
|
41
|
+
position.scale = `scale(${s})`;
|
|
42
|
+
const styleKey = ["top", "bottom", "left", "right"];
|
|
43
|
+
styleKey.forEach((key) => {
|
|
44
|
+
const val = props[key];
|
|
45
|
+
if (props.unit === "%") {
|
|
46
|
+
position[key] = val == void 0 ? "auto" : `${val}${props.unit}`;
|
|
47
|
+
} else {
|
|
48
|
+
position[key] = val == void 0 ? "auto" : `${val * s}${props.unit}`;
|
|
49
|
+
}
|
|
35
50
|
});
|
|
36
|
-
}, { immediate:
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
51
|
+
}, { immediate: true });
|
|
52
|
+
return (_ctx, _cache) => {
|
|
53
|
+
return openBlock(), createElementBlock("div", {
|
|
54
|
+
class: normalizeClass(["fit-container", { "fit-container-right": props.right !== void 0 }]),
|
|
55
|
+
style: normalizeStyle({ transform: position.scale, top: position.top, bottom: position.bottom, left: position.left, right: position.right })
|
|
56
|
+
}, [
|
|
57
|
+
renderSlot(_ctx.$slots, "default", {}, void 0, true)
|
|
58
|
+
], 6);
|
|
59
|
+
};
|
|
42
60
|
}
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
61
|
+
});
|
|
62
|
+
const _export_sfc = (sfc, props) => {
|
|
63
|
+
const target = sfc.__vccOpts || sfc;
|
|
64
|
+
for (const [key, val] of props) {
|
|
65
|
+
target[key] = val;
|
|
66
|
+
}
|
|
67
|
+
return target;
|
|
68
|
+
};
|
|
69
|
+
const FitContainer = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-1ab0024a"]]);
|
|
70
|
+
const FitScaleKey = Symbol("FitScale");
|
|
71
|
+
function createFitScale(options = {}) {
|
|
72
|
+
const fitScale = ref(1);
|
|
51
73
|
return {
|
|
52
|
-
install(
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
74
|
+
install(app) {
|
|
75
|
+
const rootEl = typeof options.target === "string" ? document.querySelector(options.target) : options.target;
|
|
76
|
+
const target = rootEl || document.querySelector("#app");
|
|
77
|
+
observeScale(target, options.designHeight ?? 1080, (v) => {
|
|
78
|
+
fitScale.value = v;
|
|
79
|
+
});
|
|
80
|
+
app.provide(FitScaleKey, fitScale);
|
|
81
|
+
app.config.globalProperties.$fitScale = fitScale;
|
|
82
|
+
app.component("FitContainer", FitContainer);
|
|
57
83
|
}
|
|
58
84
|
};
|
|
59
85
|
}
|
|
60
|
-
function
|
|
61
|
-
|
|
86
|
+
function useFitScale() {
|
|
87
|
+
const injectedFitScale = inject(FitScaleKey, ref(1));
|
|
88
|
+
return injectedFitScale;
|
|
62
89
|
}
|
|
63
90
|
export {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
91
|
+
FitContainer,
|
|
92
|
+
FitScaleKey,
|
|
93
|
+
createFitScale,
|
|
94
|
+
useFitScale
|
|
68
95
|
};
|
package/dist/fitscale.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vue")):"function"==typeof define&&define.amd?define(["exports","vue"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).FitScale={},e.Vue)}(this,function(e,t){"use strict";const o=(e=>{const t=e.__vccOpts||e;for(const[o,n]of[["__scopeId","data-v-1ab0024a"]])t[o]=n;return t})(t.defineComponent({__name:"FitContainer",props:{scale:{type:Number,default:0},top:{type:Number},bottom:{type:Number},left:{type:Number},right:{type:Number},unit:{type:String,default:"px"}},setup(e){const o=t.reactive({scale:"scale(1)",top:"auto",bottom:"auto",left:"auto",right:"auto"}),i=e,r=t.inject(n,t.ref(1));return t.watch([()=>i.scale,r],()=>{const e=i.scale&&i.scale>0?i.scale:(null==r?void 0:r.value)??1;o.scale=`scale(${e})`,["top","bottom","left","right"].forEach(t=>{const n=i[t];"%"===i.unit?o[t]=null==n?"auto":`${n}${i.unit}`:o[t]=null==n?"auto":`${n*e}${i.unit}`})},{immediate:!0}),(e,n)=>(t.openBlock(),t.createElementBlock("div",{class:t.normalizeClass(["fit-container",{"fit-container-right":void 0!==i.right}]),style:t.normalizeStyle({transform:o.scale,top:o.top,bottom:o.bottom,left:o.left,right:o.right})},[t.renderSlot(e.$slots,"default",{},void 0,!0)],6))}})),n=Symbol("FitScale");e.FitContainer=o,e.FitScaleKey=n,e.createFitScale=function(e={}){const i=t.ref(1);return{install(t){var r,a,l;r=("string"==typeof e.target?document.querySelector(e.target):e.target)||document.querySelector("#app"),a=e.designHeight??1080,l=e=>{i.value=e},new ResizeObserver(e=>{for(const t of e){const e=t.contentRect.height,o=t.contentRect.width;let n;n=o/e<1.7666666666666666?o/1920*.98:e/a,l(n)}}).observe(r),t.provide(n,i),t.config.globalProperties.$fitScale=i,t.component("FitContainer",o)}}},e.useFitScale=function(){return t.inject(n,t.ref(1))},Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vfit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "A tiny Vue 3 plugin to auto-fit UI scale based on container size, plus a FitContainer component for easy positioning.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@vitejs/plugin-vue": "^5.0.4",
|
|
27
27
|
"typescript": "^5.6.3",
|
|
28
|
-
"vite": "^5.4.0"
|
|
28
|
+
"vite": "^5.4.0",
|
|
29
|
+
"terser": "^5.31.0"
|
|
29
30
|
}
|
|
30
31
|
}
|