Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components): [carousel] height is adaptive when height is auto #11392

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
cf0739f
fix(components): [carousel] :height is auto and card direction vertical
qq282126990 Jan 16, 2023
23c729d
fix(components): [carousel] :height is auto and card direction vertical
qq282126990 Jan 16, 2023
0066489
Merge branch 'dev' into dev
qq282126990 Jan 18, 2023
c8966e9
Merge branch 'dev' into dev
qq282126990 Jan 20, 2023
486568e
Merge branch 'dev' into dev
qq282126990 Jan 29, 2023
26533c0
Merge branch 'element-plus:dev' into dev
qq282126990 Jan 30, 2023
9c1cfa5
Merge branch 'dev' into dev
qq282126990 Jan 31, 2023
6816636
Merge branch 'element-plus:dev' into dev
qq282126990 Feb 1, 2023
5626d4d
Merge branch 'dev' into dev
qq282126990 Feb 1, 2023
80618b6
Merge branch 'element-plus:dev' into dev
qq282126990 Feb 2, 2023
e30fa47
fix(components): [carousel] Added function: height is adaptive
qq282126990 Feb 2, 2023
01f5323
Merge branch 'carousel-height-auto' of https://github.com/qq282126990…
qq282126990 Feb 2, 2023
21a5338
fix(components): [carousel] card direction vertical
qq282126990 Feb 2, 2023
18dd9c5
fix(components): [carousel] Added function: height is adaptive
qq282126990 Feb 2, 2023
cc4b4af
fix(components): [carousel] Added function: card direction vertical
qq282126990 Feb 2, 2023
40b0d13
fix(components): [carousel] Added function: height is adaptive
qq282126990 Feb 2, 2023
7a2f42c
Merge branch 'dev' into card-vertical
qq282126990 Feb 2, 2023
71d162f
Merge branch 'dev' into carousel-height-auto
qq282126990 Feb 2, 2023
39dc68f
feat(components): [carousel] height is adaptive when height is auto
qq282126990 Feb 2, 2023
a519f2a
feat(components): [carousel] height is adaptive when height is auto
qq282126990 Feb 3, 2023
8afb9ff
Merge branch 'dev' into carousel-height-auto
qq282126990 Feb 4, 2023
d6ef2f3
Merge branch 'dev' into carousel-height-auto
qq282126990 Feb 6, 2023
69871f8
Merge branch 'dev' into carousel-height-auto
qq282126990 Feb 9, 2023
d1f1d2e
Merge branch 'dev' into carousel-height-auto
qq282126990 Feb 14, 2023
f362e21
Merge branch 'dev' into carousel-height-auto
qq282126990 Feb 17, 2023
03a6352
fix: tokens carousel
qq282126990 Apr 10, 2023
24ccf85
fix: carousel
qq282126990 Apr 10, 2023
4292f51
Merge branch 'card-vertical' into carousel-height-auto
qq282126990 Apr 10, 2023
7171a5b
fix: cascader.md
qq282126990 Apr 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/en-US/component/carousel.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ carousel/arrows

:::

## Auto height

When the `height` of `carousel` is set to `auto`, the `carousel` height will be automatically set according to the height of the `carousel item`

:::demo

carousel/auto-height

:::

## Card mode

When a page is wide enough but has limited height, you can activate card mode for carousels
Expand Down
43 changes: 43 additions & 0 deletions docs/examples/carousel/auto-height.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<template>
<div class="block text-center" style="height: 300px">
<span class="demonstration">each carousel-item has a different height</span>
<el-carousel height="auto" autoplay style="overflow: hidden">
<el-carousel-item style="height: 100px">
<h3 class="small justify-center" text="2xl">height 100px</h3>
</el-carousel-item>
<el-carousel-item style="height: 200px">
<h3 class="small justify-center" text="2xl">height 200px</h3>
</el-carousel-item>
<el-carousel-item style="height: 300px">
<h3 class="small justify-center" text="2xl">height 300px</h3>
</el-carousel-item>
</el-carousel>
</div>
</template>

<style scoped>
.carousel-item {
color: #475669;
opacity: 0.75;
margin: 0;
text-align: center;
}

.el-carousel__item h3 {
color: #475669;
opacity: 0.75;
display: flex;
align-items: center;
margin: 0;
text-align: center;
height: 100%;
}

.el-carousel__item:nth-child(2n) {
background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
background-color: #d3dce6;
}
</style>
2 changes: 2 additions & 0 deletions packages/components/carousel/src/carousel-item.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div
v-show="ready"
ref="carouselItemRef"
:class="[
ns.e('item'),
ns.is('active', active),
Expand Down Expand Up @@ -34,6 +35,7 @@ const ns = useNamespace('carousel')
const COMPONENT_NAME = 'ElCarouselItem'
// inject
const {
carouselItemRef,
active,
animating,
hover,
Expand Down
3 changes: 2 additions & 1 deletion packages/components/carousel/src/carousel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@mouseenter.stop="handleMouseEnter"
@mouseleave.stop="handleMouseLeave"
>
<div :class="ns.e('container')" :style="{ height: height }">
<div :class="ns.e('container')" :style="containerStyle">
<transition v-if="arrowDisplay" name="carousel-arrow-left">
<button
v-show="
Expand Down Expand Up @@ -84,6 +84,7 @@ const {
hover,
isCardType,
items,
containerStyle,
handleButtonEnter,
handleButtonLeave,
handleIndicatorClick,
Expand Down
6 changes: 6 additions & 0 deletions packages/components/carousel/src/use-carousel-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const useCarouselItem = (

const CARD_SCALE = 0.83

const carouselItemRef = ref<HTMLElement>()
const hover = ref(false)
const translate = ref(0)
const scale = ref(1)
Expand Down Expand Up @@ -125,6 +126,10 @@ export const useCarouselItem = (
}

ready.value = true

if (isActive && carouselItemRef.value) {
carouselContext.setContainerHeight(carouselItemRef.value.offsetHeight)
}
}

function handleItemClick() {
Expand Down Expand Up @@ -159,6 +164,7 @@ export const useCarouselItem = (
})

return {
carouselItemRef,
active,
animating,
hover,
Expand Down
19 changes: 19 additions & 0 deletions packages/components/carousel/src/use-carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const useCarousel = (
const timer = ref<ReturnType<typeof setInterval> | null>(null)
const hover = ref(false)
const root = ref<HTMLDivElement>()
const containerHeight = ref<number>(0)

// computed
const arrowDisplay = computed(
Expand All @@ -52,6 +53,16 @@ export const useCarousel = (

const isCardType = computed(() => props.type === 'card')
const isVertical = computed(() => props.direction === 'vertical')
const containerStyle = computed(() => {
if (props.height === 'auto') {
return {
height: `${containerHeight.value}px`,
}
}
return {
height: props.height,
}
})

// methods
const throttledArrowClick = throttle(
Expand Down Expand Up @@ -192,6 +203,12 @@ export const useCarousel = (
startTimer()
}

function setContainerHeight(height: number) {
if (props.height === 'auto') {
containerHeight.value = height
}
}
qq282126990 marked this conversation as resolved.
Show resolved Hide resolved

// watch
watch(
() => activeIndex.value,
Expand Down Expand Up @@ -253,6 +270,7 @@ export const useCarousel = (
addItem,
removeItem,
setActiveItem,
setContainerHeight,
})

return {
Expand All @@ -263,6 +281,7 @@ export const useCarousel = (
hover,
isCardType,
items,
containerStyle,
handleButtonEnter,
handleButtonLeave,
handleIndicatorClick,
Expand Down
1 change: 1 addition & 0 deletions packages/tokens/carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type CarouselContext = {
addItem: (item: CarouselItemContext) => void
removeItem: (uid: number) => void
setActiveItem: (index: number) => void
setContainerHeight: (height: number) => void
}

export const carouselContextKey: InjectionKey<CarouselContext> =
Expand Down