diff --git a/src/List.ts b/src/List.ts index 81eef3ba..d321ad60 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 */ public 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 6c2e041e..ea3e1216 100644 --- a/src/ScrollBox.ts +++ b/src/ScrollBox.ts @@ -9,6 +9,7 @@ import { Point, Ticker, } from 'pixi.js'; +import type { ListOptions, ListType } from './List'; import { List } from './List'; import { Trackpad } from './utils/trackpad/Trackpad'; @@ -20,17 +21,12 @@ 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; globalScroll?: boolean; shiftScroll?: boolean; -}; +} & Omit; /** * Scrollable view, for arranging lists of Pixi container-based elements. @@ -133,9 +129,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(); @@ -146,8 +139,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); @@ -248,8 +246,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; @@ -425,12 +423,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; } /** @@ -451,9 +449,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; @@ -484,8 +479,8 @@ export class ScrollBox extends Container .roundRect( 0, 0, - this.__width + horPadding, - this.__height + verPadding, + this.__width, + this.__height, this.options.radius | 0, ) .fill({ @@ -511,12 +506,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') { @@ -685,7 +682,7 @@ export class ScrollBox extends Container ? this.__width - target.x - target.width - - this.options.horPadding + - this.list.rightPadding : 0; this._trackpad.yAxis.value @@ -693,7 +690,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 67f564cf..daeff644 100644 --- a/src/stories/list/ListGraphics.stories.ts +++ b/src/stories/list/ListGraphics.stories.ts @@ -15,8 +15,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, @@ -30,8 +31,9 @@ export const UseGraphics: StoryFn