[Feat]: Allow AutoHeight plugin to do auto width when using y axis #1107
Unanswered
christopherpickering
asked this question in
Ideas
Replies: 1 comment 1 reply
-
Here's a modification of th auto height plugin that works for width: import type {
CreatePluginType,
EmblaCarouselType,
EmblaEventType,
} from "embla-carousel";
import type { OptionsType } from "./Options";
declare module "embla-carousel" {
interface EmblaPluginsType {
AutoWidth: AutoWidthType;
}
}
export type AutoWidthType = CreatePluginType<{}, OptionsType>;
export type AutoWidthOptionsType = AutoWidthType["options"];
function AutoWidth(userOptions: AutoWidthOptionsType = {}): AutoWidthType {
let emblaApi: EmblaCarouselType;
let slideWidths: number[] = [];
const widthEvents: EmblaEventType[] = ["select", "slideFocus"];
function init(emblaApiInstance: EmblaCarouselType): void {
emblaApi = emblaApiInstance;
const {
options: { axis },
slideRects,
} = emblaApi.internalEngine();
if (axis === "x") return;
slideWidths = slideRects.map((slideRect) => slideRect.width);
widthEvents.forEach((evt) => emblaApi.on(evt, setContainerWidth));
setContainerWidth();
}
function destroy(): void {
widthEvents.forEach((evt) => emblaApi.off(evt, setContainerWidth));
const container = emblaApi.containerNode();
container.style.width = "";
if (!container.getAttribute("style")) container.removeAttribute("style");
}
function widestInView(): number | null {
const { slideRegistry } = emblaApi.internalEngine();
const selectedIndexes = slideRegistry[emblaApi.selectedScrollSnap()];
if (!selectedIndexes) return null;
return selectedIndexes
.map((index) => slideWidths[index])
.reduce((a, b) => Math.max(a, b), 0);
}
function setContainerWidth(): void {
const width = widestInView();
console.log("width", width)
if (width === null) return;
emblaApi.containerNode().style.width = `${widestInView()}px`;
}
const self: AutoWidthType = {
name: "AutoWidth",
options: userOptions,
init,
destroy,
};
return self;
}
declare namespace AutoWidth {
let globalOptions: AutoWidthOptionsType | undefined;
}
AutoWidth.globalOptions = undefined;
export default AutoWidth; The css of the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Which variants of Embla Carousel are relevant to this feature request?
Feature description
When using the Y axis it would be cool to let the carousel do auto width using the auto height plugin.
Additional Context
Additional details here...
Before submitting
Beta Was this translation helpful? Give feedback.
All reactions