@egjs/flicking 4.8.1 → 4.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@egjs/flicking",
3
- "version": "4.8.1",
3
+ "version": "4.9.0",
4
4
  "description": "Everyday 30 million people experience. It's reliable, flexible and extendable carousel.",
5
5
  "main": "dist/flicking.js",
6
6
  "module": "dist/flicking.esm.js",
package/src/Flicking.ts CHANGED
@@ -90,6 +90,7 @@ export interface FlickingOptions {
90
90
  useResizeObserver: boolean;
91
91
  resizeDebounce: number;
92
92
  maxResizeDebounce: number;
93
+ useFractionalSize: boolean;
93
94
  externalRenderer: ExternalRenderer | null;
94
95
 
95
96
  // @deprecated
@@ -164,6 +165,7 @@ class Flicking extends Component<FlickingEvents> {
164
165
  private _useResizeObserver: FlickingOptions["useResizeObserver"];
165
166
  private _resizeDebounce: FlickingOptions["resizeDebounce"];
166
167
  private _maxResizeDebounce: FlickingOptions["maxResizeDebounce"];
168
+ private _useFractionalSize: FlickingOptions["useFractionalSize"];
167
169
  private _externalRenderer: FlickingOptions["externalRenderer"];
168
170
  private _renderExternal: FlickingOptions["renderExternal"];
169
171
 
@@ -665,6 +667,17 @@ class Flicking extends Component<FlickingEvents> {
665
667
  * @default 100
666
668
  */
667
669
  public get maxResizeDebounce() { return this._maxResizeDebounce; }
670
+ /**
671
+ * By enabling this, Flicking will calculate all internal size with CSS width computed with getComputedStyle.
672
+ * This can prevent 1px offset issue in some cases where panel size has the fractional part.
673
+ * All sizes will have the original size before CSS {@link https://developer.mozilla.org/en-US/docs/Web/CSS/transform transform} is applied on the element.
674
+ * @ko 이 옵션을 활성화할 경우, Flicking은 내부의 모든 크기를 {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect getBoundingClientRect}를 이용하여 계산합니다.
675
+ * 이를 통해, 패널 크기에 소수점을 포함할 경우에 발생할 수 있는 일부 1px 오프셋 이슈를 해결 가능합니다.
676
+ * 모든 크기는 CSS {@link https://developer.mozilla.org/en-US/docs/Web/CSS/transform transform}이 엘리먼트에 적용되기 이전의 크기를 사용할 것입니다.
677
+ * @type {boolean}
678
+ * @default false
679
+ */
680
+ public get useFractionalSize() { return this._useFractionalSize; }
668
681
  /**
669
682
  * This is an option for the frameworks(React, Vue, Angular, ...). Don't set it as it's automatically managed by Flicking.
670
683
  * @ko 프레임워크(React, Vue, Angular, ...)에서만 사용하는 옵션으로, 자동으로 설정되므로 따로 사용하실 필요 없습니다!
@@ -814,6 +827,7 @@ class Flicking extends Component<FlickingEvents> {
814
827
  useResizeObserver = true,
815
828
  resizeDebounce = 0,
816
829
  maxResizeDebounce = 100,
830
+ useFractionalSize = false,
817
831
  externalRenderer = null,
818
832
  renderExternal = null
819
833
  }: Partial<FlickingOptions> = {}) {
@@ -856,11 +870,12 @@ class Flicking extends Component<FlickingEvents> {
856
870
  this._useResizeObserver = useResizeObserver;
857
871
  this._resizeDebounce = resizeDebounce;
858
872
  this._maxResizeDebounce = maxResizeDebounce;
873
+ this._useFractionalSize = useFractionalSize;
859
874
  this._externalRenderer = externalRenderer;
860
875
  this._renderExternal = renderExternal;
861
876
 
862
877
  // Create core components
863
- this._viewport = new Viewport(getElement(root));
878
+ this._viewport = new Viewport(this, getElement(root));
864
879
  this._autoResizer = new AutoResizer(this);
865
880
  this._renderer = this._createRenderer();
866
881
  this._camera = this._createCamera();
@@ -2,13 +2,15 @@
2
2
  * Copyright (c) 2015 NAVER Corp.
3
3
  * egjs projects are licensed under the MIT license
4
4
  */
5
- import { getStyle, isString } from "../utils";
5
+ import Flicking from "../Flicking";
6
+ import { getElementSize, getStyle, isString } from "../utils";
6
7
 
7
8
  /**
8
9
  * A component that manages viewport size
9
10
  * @ko 뷰포트 크기 정보를 담당하는 컴포넌트
10
11
  */
11
12
  class Viewport {
13
+ private _flicking: Flicking;
12
14
  private _el: HTMLElement;
13
15
  private _width: number;
14
16
  private _height: number;
@@ -57,7 +59,8 @@ class Viewport {
57
59
  /**
58
60
  * @param el A viewport element<ko>뷰포트 엘리먼트</ko>
59
61
  */
60
- public constructor(el: HTMLElement) {
62
+ public constructor(flicking: Flicking, el: HTMLElement) {
63
+ this._flicking = flicking;
61
64
  this._el = el;
62
65
  this._width = 0;
63
66
  this._height = 0;
@@ -120,9 +123,25 @@ class Viewport {
120
123
  public resize() {
121
124
  const el = this._el;
122
125
  const elStyle = getStyle(el);
126
+ const {
127
+ useFractionalSize
128
+ } = this._flicking;
129
+
130
+ this._width = getElementSize({
131
+ el,
132
+ horizontal: true,
133
+ useFractionalSize,
134
+ useOffset: false,
135
+ style: elStyle
136
+ });
137
+ this._height = getElementSize({
138
+ el,
139
+ horizontal: false,
140
+ useFractionalSize,
141
+ useOffset: false,
142
+ style: elStyle
143
+ });
123
144
 
124
- this._width = el.clientWidth;
125
- this._height = el.clientHeight;
126
145
  this._padding = {
127
146
  left: elStyle.paddingLeft ? parseFloat(elStyle.paddingLeft) : 0,
128
147
  right: elStyle.paddingRight ? parseFloat(elStyle.paddingRight) : 0,
@@ -3,7 +3,7 @@
3
3
  * egjs projects are licensed under the MIT license
4
4
  */
5
5
  import Flicking from "../../Flicking";
6
- import { getProgress, getStyle, parseAlign, setSize } from "../../utils";
6
+ import { getElementSize, getProgress, getStyle, parseAlign, setSize } from "../../utils";
7
7
  import { ALIGN, DIRECTION } from "../../const/external";
8
8
  import { LiteralUnion, ValueOf } from "../../type/internal";
9
9
 
@@ -313,7 +313,10 @@ class Panel {
313
313
  }): this {
314
314
  const el = this.element;
315
315
  const flicking = this._flicking;
316
- const horizontal = flicking.horizontal;
316
+ const {
317
+ horizontal,
318
+ useFractionalSize
319
+ } = flicking;
317
320
 
318
321
  if (cached) {
319
322
  this._size = cached.size;
@@ -322,7 +325,14 @@ class Panel {
322
325
  } else {
323
326
  const elStyle = getStyle(el);
324
327
 
325
- this._size = horizontal ? el.offsetWidth : el.offsetHeight;
328
+ this._size = getElementSize({
329
+ el,
330
+ horizontal,
331
+ useFractionalSize,
332
+ useOffset: true,
333
+ style: elStyle
334
+ });
335
+
326
336
  this._margin = horizontal
327
337
  ? {
328
338
  prev: parseFloat(elStyle.marginLeft || "0"),
@@ -331,7 +341,16 @@ class Panel {
331
341
  prev: parseFloat(elStyle.marginTop || "0"),
332
342
  next: parseFloat(elStyle.marginBottom || "0")
333
343
  };
334
- this._height = horizontal ? el.offsetHeight : this._size;
344
+
345
+ this._height = horizontal
346
+ ? getElementSize({
347
+ el,
348
+ horizontal: false,
349
+ useFractionalSize,
350
+ useOffset: true,
351
+ style: elStyle
352
+ })
353
+ : this._size;
335
354
  }
336
355
 
337
356
  this.updatePosition();
package/src/utils.ts CHANGED
@@ -300,6 +300,48 @@ export const range = (end: number): number[] => {
300
300
  return arr;
301
301
  };
302
302
 
303
+ export const getElementSize = ({
304
+ el,
305
+ horizontal,
306
+ useFractionalSize,
307
+ useOffset,
308
+ style
309
+ }: {
310
+ el: HTMLElement;
311
+ horizontal: boolean;
312
+ useFractionalSize: boolean;
313
+ useOffset: boolean;
314
+ style: CSSStyleDeclaration;
315
+ }): number => {
316
+ if (useFractionalSize) {
317
+ const baseSize = parseFloat(horizontal ? style.width : style.height);
318
+ const isBorderBoxSizing = style.boxSizing === "border-box";
319
+ const border = horizontal
320
+ ? parseFloat(style.borderLeftWidth || "0") + parseFloat(style.borderRightWidth || "0")
321
+ : parseFloat(style.borderTopWidth || "0") + parseFloat(style.borderBottomWidth || "0");
322
+
323
+ if (isBorderBoxSizing) {
324
+ return useOffset
325
+ ? baseSize
326
+ : baseSize - border;
327
+ } else {
328
+ const padding = horizontal
329
+ ? parseFloat(style.paddingLeft || "0") + parseFloat(style.paddingRight || "0")
330
+ : parseFloat(style.paddingTop || "0") + parseFloat(style.paddingBottom || "0");
331
+
332
+ return useOffset
333
+ ? baseSize + padding + border
334
+ : baseSize + padding;
335
+ }
336
+ } else {
337
+ const sizeStr = horizontal ? "Width" : "Height";
338
+
339
+ return useOffset
340
+ ? el[`offset${sizeStr}`]
341
+ : el[`client${sizeStr}`];
342
+ }
343
+ };
344
+
303
345
  export const setPrototypeOf = Object.setPrototypeOf || ((obj, proto) => {
304
346
  obj.__proto__ = proto;
305
347
  return obj;