diff --git a/src/List.ts b/src/List.ts index 04d6b080..de48fd42 100644 --- a/src/List.ts +++ b/src/List.ts @@ -5,8 +5,13 @@ export type ListType = 'horizontal' | 'vertical'; export type ListOptions = { elementsMargin?: number; children?: Container[]; + padding?: number; vertPadding?: number; horPadding?: number; + topPadding?: number; + bottomPadding?: number; + leftPadding?: number; + rightPadding?: number; items?: Container[]; }; @@ -101,6 +106,7 @@ export class List extends Container */ set elementsMargin(margin: number) { + if (!this.options) throw new Error('List has not been initiated!'); this.options.elementsMargin = margin; this.arrangeChildren(); } @@ -111,16 +117,45 @@ export class List extends Container */ get elementsMargin(): number { - return this.options.elementsMargin; + return this.options?.elementsMargin ?? 0; } /** - * Set vertical padding. + * Set padding, overriding all padding options. + * @param padding - Padding surrounding list elements and its border. + */ + set padding(padding: number) + { + if (!this.options) throw new Error('List has not been initiated!'); + this.options.padding = padding; + this.options.vertPadding = padding; + this.options.horPadding = padding; + this.options.leftPadding = padding; + this.options.rightPadding = padding; + this.options.topPadding = padding; + this.options.bottomPadding = padding; + this.arrangeChildren(); + } + + /** + * Get padding. + * @returns Padding surrounding list elements and its border. + */ + get padding(): number + { + return this.options?.padding ?? 0; + } + + /** + * Set vertical padding, overriding all top and bottom padding options. * @param padding - Vertical padding between list border and its elements. */ set vertPadding(padding: number) { + if (!this.options) throw new Error('List has not been initiated!'); this.options.vertPadding = padding; + this.options.topPadding = padding; + this.options.bottomPadding = padding; this.arrangeChildren(); } @@ -130,16 +165,19 @@ export class List extends Container */ get vertPadding(): number { - return this.options.vertPadding; + return this.options?.vertPadding ?? this.padding ?? 0; } /** - * Set horizontal padding. + * Set horizontal padding, overriding all left and right padding options. * @param padding - Horizontal padding between list border and its elements. */ set horPadding(padding: number) { + if (!this.options) throw new Error('List has not been initiated!'); this.options.horPadding = padding; + this.options.leftPadding = padding; + this.options.rightPadding = padding; this.arrangeChildren(); } @@ -149,7 +187,87 @@ export class List extends Container */ get horPadding(): number { - return this.options.horPadding; + return this.options?.horPadding ?? this.padding ?? 0; + } + + /** + * Set left padding. + * @param padding - Left padding between list border and its elements. + */ + set leftPadding(padding: number) + { + if (!this.options) throw new Error('List has not been initiated!'); + this.options.leftPadding = padding; + this.arrangeChildren(); + } + + /** + * Get left padding. + * @returns Left padding between list border and its elements. + */ + get leftPadding(): number + { + return this.options?.leftPadding ?? this.horPadding; + } + + /** + * Set right padding. + * @param padding - Right padding between list border and its elements. + */ + set rightPadding(padding: number) + { + if (!this.options) throw new Error('List has not been initiated!'); + this.options.rightPadding = padding; + this.arrangeChildren(); + } + + /** + * Get right padding. + * @returns Right padding between list border and its elements. + */ + get rightPadding(): number + { + return this.options?.rightPadding ?? this.horPadding; + } + + /** + * Set top padding. + * @param padding - Top padding between list border and its elements. + */ + set topPadding(padding: number) + { + if (!this.options) throw new Error('List has not been initiated!'); + this.options.topPadding = padding; + this.arrangeChildren(); + } + + /** + * Get top padding. + * @returns Top padding between list border and its elements. + */ + get topPadding(): number + { + return this.options?.topPadding ?? this.vertPadding; + } + + /** + * Set bottom padding. + * @param padding - Bottom padding between list border and its elements. + */ + set bottomPadding(padding: number) + { + if (!this.options) throw new Error('List has not been initiated!'); + this.options.bottomPadding = padding; + this.arrangeChildren(); + } + + /** + * Get bottom padding. + * @returns Bottom padding between list border and its elements. + */ + get bottomPadding(): number + { + return this.options?.bottomPadding ?? this.vertPadding; } /** @@ -158,15 +276,15 @@ export class List extends Container */ protected arrangeChildren() { - let x = this.options?.horPadding ?? 0; - let y = this.options?.vertPadding ?? 0; + let x = this.leftPadding; + let y = this.topPadding; const elementsMargin = this.options?.elementsMargin ?? 0; let maxWidth = this.parent?.width; - if (this.options?.horPadding) + if (this.rightPadding) { - maxWidth -= this.options.horPadding; + maxWidth -= this.rightPadding; } this.children.forEach((child, id) => @@ -194,7 +312,7 @@ export class List extends Container if (child.x + child.width >= maxWidth && id > 0) { y += elementsMargin + child.height; - x = this.options?.horPadding ?? 0; + x = this.leftPadding; child.x = x; child.y = y; diff --git a/src/ScrollBox.ts b/src/ScrollBox.ts index fd9d5b59..b2e58ab5 100644 --- a/src/ScrollBox.ts +++ b/src/ScrollBox.ts @@ -2,7 +2,7 @@ import { ColorSource, Ticker, utils, Point } from '@pixi/core'; import { Container, DisplayObject, IDestroyOptions } from '@pixi/display'; import { EventMode, FederatedPointerEvent } from '@pixi/events'; import { Graphics } from '@pixi/graphics'; -import type { ListType } from './List'; +import type { ListOptions, ListType } from './List'; import { List } from './List'; import { Trackpad } from './utils/trackpad/Trackpad'; @@ -12,15 +12,11 @@ export type ScrollBoxOptions = { background?: ColorSource; type?: ListType; radius?: number; - elementsMargin?: number; - items?: Container[]; disableDynamicRendering?: boolean; - vertPadding?: number; - horPadding?: number; padding?: number; disableEasing?: boolean; dragTrashHold?: number; -}; +} & Omit; /** * Scrollable view, for arranging lists of Pixi container-based elements. @@ -116,9 +112,6 @@ export class ScrollBox extends Container this.__width = options.width | this.background.width; this.__height = options.height | this.background.height; - options.vertPadding = options.vertPadding ?? options.padding ?? 0; - options.horPadding = options.horPadding ?? options.padding ?? 0; - if (!this.list) { this.list = new List(); @@ -129,8 +122,13 @@ export class ScrollBox extends Container this.list.init({ type: options.type, elementsMargin: options.elementsMargin, + padding: options.padding, vertPadding: options.vertPadding, horPadding: options.horPadding, + topPadding: options.topPadding, + bottomPadding: options.bottomPadding, + leftPadding: options.leftPadding, + rightPadding: options.rightPadding, }); this.addItems(options.items); @@ -234,8 +232,8 @@ export class ScrollBox extends Container const posY = item.y + list.y; if ( - posY + item.height + this.options.vertPadding >= 0 - && posY - this.options.vertPadding <= this.options.height + posY + item.height + this.list.bottomPadding >= 0 + && posY - this.list.topPadding <= this.options.height ) { isVisible = true; @@ -401,12 +399,12 @@ export class ScrollBox extends Container protected get listHeight(): number { - return this.list.height + (this.options.vertPadding * 2); + return this.list.height + this.list.topPadding + this.list.bottomPadding; } protected get listWidth(): number { - return this.list.width + (this.options.horPadding * 2); + return this.list.width + this.list.leftPadding + this.list.rightPadding; } /** Controls item positions and visibility. */ @@ -422,9 +420,6 @@ export class ScrollBox extends Container || this.lastHeight !== this.listHeight) ) { - const verPadding = this.options.vertPadding; - const horPadding = this.options.horPadding; - if (!this.options.width) { this.__width += this.listWidth; @@ -460,8 +455,8 @@ export class ScrollBox extends Container this.background.drawRoundedRect( 0, 0, - this.__width + horPadding, - this.__height + verPadding, + this.__width, + this.__height, this.options.radius | 0, ); @@ -483,12 +478,14 @@ export class ScrollBox extends Container const maxWidth = this.borderMask.width - this.list.width - - (this.options.horPadding * 2); + - this.list.leftPadding + - this.list.rightPadding; const maxHeight = this.borderMask.height - this.list.height - - (this.options.vertPadding * 2); + - this.list.topPadding + - this.list.bottomPadding; if (this.options.type === 'vertical') { @@ -528,13 +525,13 @@ export class ScrollBox extends Container } else if ( targetPos < 0 - && targetPos + this.listWidth + this.options.horPadding + && targetPos + this.listWidth + this.list.rightPadding < this.__width ) { this._trackpad.xAxis.value = this.__width - this.listWidth; } - else if (targetPos > this.options.horPadding) + else if (targetPos > this.list.leftPadding) { this._trackpad.xAxis.value = 0; } @@ -553,13 +550,13 @@ export class ScrollBox extends Container } else if ( targetPos < 0 - && targetPos + this.listHeight + this.options.vertPadding + && targetPos + this.listHeight + this.list.bottomPadding < this.__height ) { this._trackpad.yAxis.value = this.__height - this.listHeight; } - else if (targetPos > this.options.vertPadding) + else if (targetPos > this.list.topPadding) { this._trackpad.yAxis.value = 0; } @@ -664,7 +661,7 @@ export class ScrollBox extends Container ? this.__width - target.x - target.width - - this.options.horPadding + - this.list.rightPadding : 0; this._trackpad.yAxis.value @@ -672,7 +669,7 @@ export class ScrollBox extends Container ? this.__height - target.y - target.height - - this.options.vertPadding + - this.list.bottomPadding : 0; this.stopRenderHiddenItems(); diff --git a/src/stories/list/ListGraphics.stories.ts b/src/stories/list/ListGraphics.stories.ts index e734e306..b6edc6cc 100644 --- a/src/stories/list/ListGraphics.stories.ts +++ b/src/stories/list/ListGraphics.stories.ts @@ -17,8 +17,9 @@ const args = { height: 270, radius: 20, elementsMargin: 10, - vertPadding: 20, - horPadding: 20, + topPadding: 20, + leftPadding: 20, + rightPadding: 20, elementsWidth: 70, elementsHeight: 70, itemsAmount: 9, @@ -32,8 +33,9 @@ export const UseGraphics: StoryFn = ({ width, height, elementsMargin, - vertPadding, - horPadding, + topPadding, + leftPadding, + rightPadding, elementsWidth, elementsHeight, radius, @@ -70,8 +72,9 @@ export const UseGraphics: StoryFn = ({ // Component usage !!! const list = new List({ elementsMargin, - vertPadding, - horPadding, + topPadding, + leftPadding, + rightPadding, type });