-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2011 from graphcommerce-org/feature/gallery-thumb…
…nails-with-hover Gallery thumbnails (GCOM-1100)
- Loading branch information
Showing
15 changed files
with
287 additions
and
26 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 @@ | ||
--- | ||
'@graphcommerce/framer-scroller': patch | ||
--- | ||
|
||
Added a new Image Gallery as a plugin |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { useMotionValueValue } from '@graphcommerce/framer-utils' | ||
import { Image, ImageProps } from '@graphcommerce/image' | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { extendableComponent, responsiveVal } from '@graphcommerce/next-ui/Styles' | ||
import { alpha, styled, useTheme } from '@mui/material' | ||
import { m, motionValue, useTransform } from 'framer-motion' | ||
import { useEffect, useRef } from 'react' | ||
import { useScrollerContext } from '../hooks/useScrollerContext' | ||
|
||
const name = 'ScrollerThumbnail' | ||
const parts = ['thumbnail'] as const | ||
type OwnerProps = { active: boolean } | ||
|
||
const { withState } = extendableComponent<OwnerProps, typeof name, typeof parts>(name, parts) | ||
|
||
type ScrollerThumbnailProps = { | ||
idx: number | ||
image: Pick<ImageProps, 'src' | 'height' | 'width'> | ||
} | ||
|
||
const MotionBox = styled(m.div)({}) | ||
|
||
export function ScrollerThumbnail(props: ScrollerThumbnailProps) { | ||
const { idx, image } = props | ||
const { scrollerRef, scroll, getScrollSnapPositions, items } = useScrollerContext() | ||
const found = useMotionValueValue(items, (v) => v.find((_, index) => index === idx)) | ||
// This ensures that the first item in the scroller is selected by default. | ||
// The opacity property is set to 0 by default. | ||
const item = found ?? { | ||
visibility: idx === 0 ? motionValue(1) : motionValue(0), | ||
opacity: motionValue(0), | ||
} | ||
const active = useMotionValueValue(item.visibility, (v) => v >= 0.5) | ||
const theme = useTheme() | ||
|
||
const ref = useRef<HTMLDivElement>(null) | ||
|
||
const boxShadow = useTransform( | ||
item.visibility, | ||
[1, 0], | ||
[ | ||
`inset 0 0 0 2px ${theme.palette.primary.main}, 0 0 0 4px ${alpha( | ||
theme.palette.primary.main, | ||
theme.palette.action.hoverOpacity, | ||
)}`, | ||
`inset 0 0 0 2px #ffffff00, 0 0 0 4px #ffffff00`, | ||
], | ||
) | ||
|
||
const classes = withState({ active }) | ||
|
||
const scrollIntoView = () => | ||
ref.current?.scrollIntoView({ block: 'nearest', inline: 'center', behavior: 'auto' }) | ||
|
||
useEffect(() => { | ||
if (active && ref.current) { | ||
// This is a hack to ensure that the scroll animation is finished. | ||
setTimeout(() => scrollIntoView(), 1) | ||
} | ||
}, [active]) | ||
|
||
if (!image) return null | ||
|
||
return ( | ||
<MotionBox | ||
ref={ref} | ||
className={classes.thumbnail} | ||
onClick={() => { | ||
if (!scrollerRef.current) return | ||
scroll.animating.set(true) | ||
const { x } = getScrollSnapPositions() | ||
scrollerRef.current.scrollLeft = x[idx] | ||
scroll.x.set(x[idx]) | ||
scroll.animating.set(false) | ||
}} | ||
layout='position' | ||
style={{ boxShadow }} | ||
sx={{ | ||
padding: '2px', | ||
mx: `calc(${theme.spacing(1)} / 2)`, | ||
borderRadius: theme.shape.borderRadius, | ||
}} | ||
> | ||
<Image | ||
{...image} | ||
loading='eager' | ||
sx={{ | ||
height: responsiveVal(35, 90), | ||
width: 'auto', | ||
display: 'block', | ||
pointerEvents: 'none', | ||
objectFit: 'cover', | ||
borderRadius: theme.shape.borderRadius - 0.7, | ||
}} | ||
sizes={responsiveVal(35, 90)} | ||
/> | ||
</MotionBox> | ||
) | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/framer-scroller/components/ScrollerThumbnails.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,31 @@ | ||
import { ImageProps } from '@graphcommerce/image' | ||
import { ButtonProps, SxProps, Theme } from '@mui/material' | ||
import { ScrollerThumbnail } from './ScrollerThumbnail' | ||
import { ThumbnailContainer } from './ThumbnailContainer' | ||
|
||
export type ThumbnailsProps = { | ||
buttonProps?: Omit<ButtonProps, 'onClick' | 'children'> | ||
sx?: SxProps<Theme> | ||
images: Pick<ImageProps, 'src' | 'height' | 'width'>[] | ||
} | ||
|
||
const componentName = 'ScrollerThumbnails' | ||
|
||
export function ScrollerThumbnails(props: ThumbnailsProps) { | ||
const { images, sx = [], ...buttonProps } = props | ||
return ( | ||
<ThumbnailContainer sx={sx}> | ||
{images.map((item, i) => ( | ||
<ScrollerThumbnail | ||
// eslint-disable-next-line react/no-array-index-key | ||
key={`${i}-image`} | ||
idx={i} | ||
image={item} | ||
{...buttonProps} | ||
/> | ||
))} | ||
</ThumbnailContainer> | ||
) | ||
} | ||
|
||
ScrollerThumbnails.displayName = componentName |
43 changes: 43 additions & 0 deletions
43
packages/framer-scroller/components/ThumbnailContainer.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 @@ | ||
import { styled, SxProps, Theme } from '@mui/material' | ||
import { m, PanHandlers } from 'framer-motion' | ||
import React, { useRef } from 'react' | ||
|
||
const MotionBox = styled(m.div)({}) | ||
|
||
type ThumbnailContainerProps = { | ||
children: React.ReactNode | ||
sx?: SxProps<Theme> | ||
} | ||
|
||
export function ThumbnailContainer(props: ThumbnailContainerProps) { | ||
const { children, sx } = props | ||
const containerRef = useRef<HTMLDivElement>(null) | ||
const onPan: PanHandlers['onPan'] = (_, info) => { | ||
containerRef.current?.scrollBy({ left: -info.delta.x }) | ||
} | ||
|
||
return ( | ||
<MotionBox | ||
ref={containerRef} | ||
onPan={onPan} | ||
layout | ||
sx={[ | ||
{ | ||
padding: '4px', | ||
userSelect: 'none', | ||
cursor: 'grab', | ||
overflow: 'none', | ||
overflowX: 'auto', | ||
display: 'flex', | ||
scrollbarWidth: 'none', | ||
'&::-webkit-scrollbar': { | ||
display: 'none', | ||
}, | ||
}, | ||
...(Array.isArray(sx) ? sx : [sx]), | ||
]} | ||
> | ||
{children} | ||
</MotionBox> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extend input GraphCommerceConfig { | ||
""" | ||
Configuration for the SidebarGallery component | ||
""" | ||
sidebarGallery: SidebarGalleryConfig | ||
} | ||
|
||
""" | ||
Enumeration of all possible positions for the sidebar gallery thumbnails. | ||
""" | ||
enum SidebarGalleryPaginationVariant { | ||
DOTS | ||
THUMBNAILS_BOTTOM | ||
} | ||
|
||
""" | ||
SidebarGalleryConfig will contain all configuration values for the Sidebar Gallery component. | ||
""" | ||
input SidebarGalleryConfig { | ||
""" | ||
Variant used for the pagination | ||
""" | ||
paginationVariant: SidebarGalleryPaginationVariant | ||
} |
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.