Skip to content

Commit

Permalink
Merge pull request #802 from lumapps/chore/play-stability
Browse files Browse the repository at this point in the history
chore(slideshow): temporarily remove delayed visibility
  • Loading branch information
gcornut authored Mar 17, 2022
2 parents 811454f + 59d0dd2 commit f3ac5a9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 75 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Slideshow: temporarily remove slide `visibility: hidden`.

## [2.2.10][] - 2022-03-16

### Added
Expand Down
27 changes: 5 additions & 22 deletions packages/lumx-react/src/components/slideshow/Slides.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ export interface SlidesProps extends GenericProps {
/** id to be passed in into the slides */
slidesId?: string;
/** callback to change whether the slideshow is playing or not */
setIsAutoPlaying: (isAutoPlaying: boolean) => void;
/** starting visible index */
startIndexVisible: number;
/** ending visible index */
endIndexVisible: number;
toggleAutoPlay: () => void;
/** component to be rendered after the slides */
afterSlides?: React.ReactNode;
}
Expand Down Expand Up @@ -61,11 +57,8 @@ export const Slides: Comp<SlidesProps, HTMLDivElement> = forwardRef((props, ref)
fillHeight,
groupBy,
isAutoPlaying,
autoPlay,
toggleAutoPlay,
slidesId,
setIsAutoPlaying,
startIndexVisible,
endIndexVisible,
children,
afterSlides,
...forwardedProps
Expand All @@ -87,22 +80,12 @@ export const Slides: Comp<SlidesProps, HTMLDivElement> = forwardRef((props, ref)
<div
id={slidesId}
className={`${CLASSNAME}__slides`}
onMouseEnter={() => setIsAutoPlaying(false)}
onMouseLeave={() => setIsAutoPlaying(Boolean(autoPlay))}
onMouseEnter={toggleAutoPlay}
onMouseLeave={toggleAutoPlay}
aria-live={isAutoPlaying ? 'off' : 'polite'}
>
<div className={`${CLASSNAME}__wrapper`} style={wrapperStyle}>
{React.Children.map(children, (child: React.ReactNode, index: number) => {
if (React.isValidElement(child)) {
const isCurrentlyVisible = index >= startIndexVisible && index < endIndexVisible;

return React.cloneElement(child, {
isCurrentlyVisible,
});
}

return null;
})}
{children}
</div>
</div>

Expand Down
13 changes: 4 additions & 9 deletions packages/lumx-react/src/components/slideshow/Slideshow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,15 @@ export const Slideshow: Comp<SlideshowProps, HTMLDivElement> = forwardRef((props
setSlideshow,
isAutoPlaying,
slideshowSlidesId,
setIsAutoPlaying,
startIndexVisible,
endIndexVisible,
slidesCount,
onNextClick,
onPaginationClick,
onPreviousClick,
slideshow,
stopAutoPlay,
startAutoPlay,
isForcePaused,
setIsForcePaused,
toggleAutoPlay,
toggleForcePause,
} = SlideshowControls.useSlideshowControls({
activeIndex,
defaultActiveIndex: DEFAULT_PROPS.activeIndex as number,
Expand Down Expand Up @@ -113,9 +110,7 @@ export const Slideshow: Comp<SlideshowProps, HTMLDivElement> = forwardRef((props
isAutoPlaying={isAutoPlaying}
autoPlay={autoPlay}
slidesId={slideshowSlidesId}
setIsAutoPlaying={setIsAutoPlaying}
startIndexVisible={startIndexVisible}
endIndexVisible={endIndexVisible}
toggleAutoPlay={toggleAutoPlay}
interval={interval}
ref={mergeRefs(ref, setSlideshow)}
afterSlides={
Expand Down Expand Up @@ -143,7 +138,7 @@ export const Slideshow: Comp<SlideshowProps, HTMLDivElement> = forwardRef((props
autoPlay
? {
'aria-controls': slideshowSlidesId,
onClick: () => setIsForcePaused(!isForcePaused),
onClick: toggleForcePause,
...slideshowControlsProps.playButtonProps,
}
: undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,15 @@ export const ControllingSlideshow = ({ theme }: any) => {
setSlideshow,
isAutoPlaying,
slideshowSlidesId,
setIsAutoPlaying,
startIndexVisible,
endIndexVisible,
slidesCount,
onNextClick,
onPaginationClick,
onPreviousClick,
slideshow,
isForcePaused,
setIsForcePaused,
stopAutoPlay,
startAutoPlay,
toggleAutoPlay,
toggleForcePause,
} = SlideshowControls.useSlideshowControls({
activeIndex: 0,
defaultActiveIndex: 0,
Expand All @@ -73,11 +70,8 @@ export const ControllingSlideshow = ({ theme }: any) => {
ref={setSlideshow}
theme={theme}
isAutoPlaying={isAutoPlaying}
autoPlay
slidesId={slideshowSlidesId}
setIsAutoPlaying={setIsAutoPlaying}
startIndexVisible={startIndexVisible}
endIndexVisible={endIndexVisible}
toggleAutoPlay={toggleAutoPlay}
groupBy={1}
style={{ width: '50%' }}
afterSlides={
Expand All @@ -96,7 +90,7 @@ export const ControllingSlideshow = ({ theme }: any) => {
playButtonProps={{
label: 'Play/Pause',
'aria-controls': slideshowSlidesId,
onClick: () => setIsForcePaused(!isForcePaused),
onClick: toggleForcePause,
}}
paginationItemLabel={(index) => `Slide ${index}`}
/>
Expand Down
15 changes: 2 additions & 13 deletions packages/lumx-react/src/components/slideshow/SlideshowItem.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import React, { forwardRef, useState } from 'react';
import React, { forwardRef } from 'react';

import classNames from 'classnames';

import { Comp, GenericProps, getRootClassName, handleBasicClasses } from '@lumx/react/utils';

import { useDelayedVisibility } from '@lumx/react/hooks/useDelayedVisibility';

import { SLIDESHOW_TRANSITION_DURATION } from '@lumx/core/js/constants';

/**
* Defines the props of the component.
*/
Expand Down Expand Up @@ -36,12 +32,7 @@ const CLASSNAME = getRootClassName(COMPONENT_NAME);
* @return React element.
*/
export const SlideshowItem: Comp<SlideshowItemProps, HTMLDivElement> = forwardRef((props, ref) => {
const { className, children, isCurrentlyVisible = false, ...forwardedProps } = props;
const [isVisible, setIsVisible] = useState<boolean>(isCurrentlyVisible);

useDelayedVisibility(isCurrentlyVisible, SLIDESHOW_TRANSITION_DURATION, (isNowVisible) => {
setIsVisible(isNowVisible);
});
const { className, children, ...forwardedProps } = props;

return (
<div
Expand All @@ -55,8 +46,6 @@ export const SlideshowItem: Comp<SlideshowItemProps, HTMLDivElement> = forwardRe
aria-roledescription="slide"
role="group"
{...forwardedProps}
style={!isVisible ? { visibility: 'hidden', ...(forwardedProps.style || {}) } : forwardedProps.style || {}}
aria-hidden={!isVisible}
>
{children}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ Array [
</div>
}
autoPlay={false}
endIndexVisible={1}
groupBy={1}
id="slideshow1"
interval={1000}
isAutoPlaying={false}
setIsAutoPlaying={[Function]}
slidesId="slideshow-slides2"
startIndexVisible={0}
style={
Object {
"width": "50%",
}
}
theme="light"
toggleAutoPlay={[Function]}
>
<SlideshowItem
key="/demo-assets/landscape1.jpg-0"
Expand Down
49 changes: 31 additions & 18 deletions packages/lumx-react/src/hooks/useSlideshowControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export interface UseSlideshowControls {
isAutoPlaying: boolean;
/** whether the slideshow was force paused or not */
isForcePaused: boolean;
/** callback to enable/disable the force pause feature */
setIsForcePaused: (isForcePaused: boolean) => void;
/** callback to change whether the slideshow is autoplaying or not */
setIsAutoPlaying: (isAutoPlaying: boolean) => void;
toggleAutoPlay: () => void;
/** calback to change whether the slideshow should be force paused or not */
toggleForcePause: () => void;
/** current active slide index */
activeIndex: number;
/** set the current index as the active one */
Expand Down Expand Up @@ -141,10 +141,19 @@ export const useSlideshowControls = ({
}
}, [currentIndex, slidesCount, defaultActiveIndex]);

const startAutoPlay = () => {
setIsAutoPlaying(Boolean(autoPlay));
};

const stopAutoPlay = () => {
setIsAutoPlaying(false);
};

// Handle click on a bullet to go to a specific slide.
const onPaginationClick = useCallback(
(index: number) => {
setIsAutoPlaying(false);
stopAutoPlay();
setIsForcePaused(true);

if (index >= 0 && index < slidesCount) {
setCurrentIndex(index);
Expand All @@ -156,7 +165,8 @@ export const useSlideshowControls = ({
// Handle click or keyboard event to go to next slide.
const onNextClick = useCallback(
(loopback = true) => {
setIsAutoPlaying(false);
stopAutoPlay();
setIsForcePaused(true);
goToNextSlide(loopback);
},
[goToNextSlide],
Expand All @@ -165,7 +175,8 @@ export const useSlideshowControls = ({
// Handle click or keyboard event to go to previous slide.
const onPreviousClick = useCallback(
(loopback = true) => {
setIsAutoPlaying(false);
stopAutoPlay();
setIsForcePaused(true);
goToPreviousSlide(loopback);
},
[goToPreviousSlide],
Expand All @@ -185,21 +196,23 @@ export const useSlideshowControls = ({
const slideshowId = useMemo(() => id || uniqueId('slideshow'), [id]);
const slideshowSlidesId = useMemo(() => slidesId || uniqueId('slideshow-slides'), [slidesId]);

const startAutoPlay = () => {
setIsAutoPlaying(Boolean(autoPlay));
const toggleAutoPlay = () => {
if (isSlideshowAutoPlaying) {
stopAutoPlay();
} else {
startAutoPlay();
}
};

const stopAutoPlay = () => {
setIsAutoPlaying(false);
};
const toggleForcePause = () => {
const shouldBePaused = !isForcePaused;

const forcePause = (isPaused: boolean) => {
setIsForcePaused(isPaused);
setIsForcePaused(shouldBePaused);

if (isPaused) {
stopAutoPlay();
} else {
if (!shouldBePaused) {
startAutoPlay();
} else {
stopAutoPlay();
}
};

Expand All @@ -218,13 +231,13 @@ export const useSlideshowControls = ({
onNextClick,
onPaginationClick,
isAutoPlaying: isSlideshowAutoPlaying,
setIsAutoPlaying,
toggleAutoPlay,
activeIndex: currentIndex,
slidesCount,
setActiveIndex: setCurrentIndex,
startAutoPlay,
stopAutoPlay,
isForcePaused,
setIsForcePaused: forcePause,
toggleForcePause,
};
};

0 comments on commit f3ac5a9

Please sign in to comment.