@carbon/ibm-products-web-components
Version:
Carbon for IBM Products Web Components
96 lines (94 loc) • 2.52 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { signal } from "@lit-labs/signals";
//#region src/components/tearsheet-preview/stack-signal.ts
/**
* @license
*
* Copyright IBM Corp. 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const bufferMap = {
sm: .5,
md: .75,
lg: 1
};
var StackManager = class {
#state = signal({
stack: [],
containers: /* @__PURE__ */ new Map(),
stackStepSize: "lg"
});
get state() {
return this.#state.get();
}
setStackStepSize(size) {
this.#state.set({
...this.#state.get(),
stackStepSize: size
});
}
notifyStack(id, open, container) {
const currentState = this.#state.get();
const newContainers = new Map(currentState.containers);
if (open && container) {
newContainers.set(id, container);
const newStack = currentState.stack.includes(id) ? [...currentState.stack.filter((i) => i !== id), id] : [...currentState.stack, id];
this.#state.set({
...currentState,
stack: newStack,
containers: newContainers
});
} else {
newContainers.delete(id);
this.#state.set({
...currentState,
stack: currentState.stack.filter((i) => i !== id),
containers: newContainers
});
}
}
getDepth(id) {
const { stack } = this.#state.get();
const index = stack.indexOf(id);
if (index === -1) return -1;
return stack.length - 1 - index;
}
getScaleFactor(id) {
const depth = this.getDepth(id);
const { stackStepSize, containers } = this.#state.get();
const container = containers.get(id);
if (depth === -1 || !container) return 1;
const buffer = bufferMap[stackStepSize];
const bufferInPx = this.remToPx(buffer);
const width = container.offsetWidth;
return (width - bufferInPx * 2 * depth) / width;
}
getBlockSizeChange(id) {
const depth = this.getDepth(id);
const { stackStepSize } = this.#state.get();
if (depth === -1) return "0px";
const buffer = bufferMap[stackStepSize];
return `${this.remToPx(buffer) * depth}px`;
}
remToPx(rem) {
return rem * parseFloat(getComputedStyle(document.documentElement).fontSize);
}
reset() {
this.#state.set({
stack: [],
containers: /* @__PURE__ */ new Map(),
stackStepSize: "lg"
});
}
};
const stackManager = new StackManager();
//#endregion
export { stackManager };
//# sourceMappingURL=stack-signal.js.map