-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor setup of carousel component (#19341)
* Restructure carousel setup. * Disable navigation buttons in news carousel when there are no more slides. * Adding changelog. * Add example. * Fixing disabled state.
- Loading branch information
Showing
15 changed files
with
249 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type="f" | ||
message="Fix issue with scroll position which could occur in news section on the startpage when scrolling by dragging cards." | ||
|
||
issues=["19337"] | ||
pulls=["19341"] |
18 changes: 18 additions & 0 deletions
18
graylog2-web-interface/src/components/common/Carousel/Carousel.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#### Simple Usage | ||
|
||
```tsx | ||
import Carousel from './Carousel'; | ||
import CarouselProvider from './CarouselProvider'; | ||
|
||
<CarouselProvider> | ||
<Carousel> | ||
<Carousel.Slide>Slide 1</Carousel.Slide> | ||
<Carousel.Slide>Slide 2</Carousel.Slide> | ||
<Carousel.Slide>Slide 3</Carousel.Slide> | ||
<Carousel.Slide>Slide 4</Carousel.Slide> | ||
<Carousel.Slide>Slide 5</Carousel.Slide> | ||
<Carousel.Slide>Slide 6</Carousel.Slide> | ||
<Carousel.Slide>Slide 7</Carousel.Slide> | ||
</Carousel> | ||
</CarouselProvider> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
graylog2-web-interface/src/components/common/Carousel/CarouselContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (C) 2020 Graylog, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import type { Record } from 'immutable'; | ||
import type { EmblaCarouselType } from 'embla-carousel'; | ||
|
||
import { singleton } from 'logic/singleton'; | ||
|
||
type CarouselId = string | ||
|
||
const CarouselContext = React.createContext<Record<CarouselId, { ref: React.Ref<HTMLDivElement>, api: EmblaCarouselType | undefined }> | undefined>(undefined); | ||
|
||
export default singleton('contexts.CarouselContext', () => CarouselContext); |
43 changes: 43 additions & 0 deletions
43
graylog2-web-interface/src/components/common/Carousel/CarouselProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2020 Graylog, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
*/ | ||
import * as React from 'react'; | ||
import { useMemo, useContext } from 'react'; | ||
import useEmblaCarousel from 'embla-carousel-react'; | ||
|
||
import CarouselContext from './CarouselContext'; | ||
|
||
type Props = React.PropsWithChildren<{ | ||
carouselId: string | ||
}> | ||
|
||
const CarouselProvider = ({ carouselId, children } : Props) => { | ||
const existingContextValue = useContext(CarouselContext); | ||
const [ref, api] = useEmblaCarousel({ containScroll: 'trimSnaps' }); | ||
|
||
const value = useMemo(() => ({ | ||
...(existingContextValue ?? {}), | ||
[carouselId]: { ref, api }, | ||
}), [api, carouselId, existingContextValue, ref]); | ||
|
||
return ( | ||
<CarouselContext.Provider value={value}> | ||
{children} | ||
</CarouselContext.Provider> | ||
); | ||
}; | ||
|
||
export default CarouselProvider; |
File renamed without changes.
50 changes: 50 additions & 0 deletions
50
graylog2-web-interface/src/components/common/Carousel/hooks/useCarouselActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (C) 2020 Graylog, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
*/ | ||
|
||
import { useState, useEffect, useCallback } from 'react'; | ||
|
||
import useCarouselApi from './useCarouselApi'; | ||
|
||
const useCarouselActions = (carouselId: string) => { | ||
const carouselApi = useCarouselApi(carouselId); | ||
const canScrollPrev = useCallback(() => !!carouselApi?.canScrollPrev(), [carouselApi]); | ||
const canScrollNext = useCallback(() => !!carouselApi?.canScrollNext(), [carouselApi]); | ||
|
||
const [nextBtnDisabled, setNextBtnDisabled] = useState(false); | ||
const [prevBtnDisabled, setPrevBtnDisabled] = useState(false); | ||
|
||
const onSelect = useCallback(() => { | ||
setPrevBtnDisabled(!canScrollPrev()); | ||
setNextBtnDisabled(!canScrollNext()); | ||
}, [canScrollNext, canScrollPrev]); | ||
|
||
useEffect(() => { | ||
if (carouselApi) { | ||
carouselApi.on('reInit', onSelect); | ||
carouselApi.on('select', onSelect); | ||
} | ||
}, [carouselApi, onSelect]); | ||
|
||
return { | ||
scrollNext: carouselApi?.scrollNext ? carouselApi.scrollNext : () => {}, | ||
scrollPrev: carouselApi?.scrollPrev ? carouselApi.scrollPrev : () => {}, | ||
nextBtnDisabled, | ||
prevBtnDisabled, | ||
}; | ||
}; | ||
|
||
export default useCarouselActions; |
36 changes: 36 additions & 0 deletions
36
graylog2-web-interface/src/components/common/Carousel/hooks/useCarouselApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2020 Graylog, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
*/ | ||
|
||
import { useContext } from 'react'; | ||
|
||
import CarouselContext from '../CarouselContext'; | ||
|
||
const useCarouselApi = (carouselId: string) => { | ||
const carouselContext = useContext(CarouselContext); | ||
|
||
if (!carouselContext) { | ||
throw new Error('useCarouselApi hook needs to be used inside CarouselApiProvider.'); | ||
} | ||
|
||
if (!carouselContext[carouselId]) { | ||
throw new Error(`CarouselContext does not contain anything for carousel id ${carouselId}`); | ||
} | ||
|
||
return carouselContext[carouselId].api; | ||
}; | ||
|
||
export default useCarouselApi; |
24 changes: 24 additions & 0 deletions
24
graylog2-web-interface/src/components/common/Carousel/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (C) 2020 Graylog, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
*/ | ||
|
||
import Carousel from './Carousel'; | ||
|
||
export { default as useCarouselApi } from './hooks/useCarouselApi'; | ||
export { default as useCarouselActions } from './hooks/useCarouselActions'; | ||
export { default as CarouselProvider } from './CarouselProvider'; | ||
|
||
export default Carousel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.