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(Gallery): add handle click to bullet #7213

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
margin-block: 0;
margin-inline: 3px;
opacity: var(--vkui--opacity_disable_accessibility);
border: 0;
outline: 0;
padding: 0;
cursor: pointer;
}

.BaseGallery__bullet--active {
Expand Down
29 changes: 11 additions & 18 deletions packages/vkui/src/components/BaseGallery/BaseGallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useIsomorphicLayoutEffect } from '../../lib/useIsomorphicLayoutEffect';
import { RootComponent } from '../RootComponent/RootComponent';
import { ScrollArrow } from '../ScrollArrow/ScrollArrow';
import { Touch, TouchEvent } from '../Touch/Touch';
import { GalleryBullets } from './GalleryBullets/GalleryBullets';
import { calcMax, calcMin } from './helpers';
import { BaseGalleryProps, GallerySlidesState, LayoutState, ShiftingState } from './types';
import styles from './BaseGallery.module.css';
Expand All @@ -32,10 +33,6 @@ const SHIFT_DEFAULT_STATE = {
indent: 0,
};

const stylesBullets = {
dark: styles['BaseGallery__bullets--dark'],
light: styles['BaseGallery__bullets--light'],
};
export const BaseGallery = ({
bullets = false,
getRootRef,
Expand All @@ -52,6 +49,8 @@ export const BaseGallery = ({
showArrows,
getRef,
arrowSize = 'l',
bulletsClassName,
slideClassName,
...restProps
}: BaseGalleryProps): React.ReactNode => {
const slidesStore = React.useRef<Record<string, HTMLDivElement | null>>({});
Expand Down Expand Up @@ -338,7 +337,7 @@ export const BaseGallery = ({
<div className={styles['BaseGallery__layer']} style={layerStyle}>
{React.Children.map(children, (item: React.ReactNode, i: number) => (
<div
className={styles['BaseGallery__slide']}
className={classNames(styles['BaseGallery__slide'], slideClassName)}
key={`slide-${i}`}
ref={(el) => setSlideRef(el, i)}
>
Expand All @@ -349,20 +348,14 @@ export const BaseGallery = ({
</Touch>

{bullets && (
<div
aria-hidden
className={classNames(styles['BaseGallery__bullets'], stylesBullets[bullets])}
<GalleryBullets
bullets={bullets}
slideIndex={slideIndex}
onChange={onChange}
bulletsClassName={bulletsClassName}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Этот проп я бы просто назвал className у самого компонента GalleryBullets.

>
{React.Children.map(children, (_item: React.ReactNode, index: number) => (
<div
className={classNames(
styles['BaseGallery__bullet'],
index === slideIndex && styles['BaseGallery__bullet--active'],
)}
key={index}
/>
))}
</div>
{children}
</GalleryBullets>
)}

{showArrows && hasPointer && canSlideLeft && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ import { warnOnce } from '../../../lib/warnOnce';
import { RootComponent } from '../../RootComponent/RootComponent';
import { ScrollArrow } from '../../ScrollArrow/ScrollArrow';
import { Touch, TouchEvent } from '../../Touch/Touch';
import { GalleryBullets } from '../GalleryBullets/GalleryBullets';
import { BaseGalleryProps, GallerySlidesState } from '../types';
import { ANIMATION_DURATION, CONTROL_ELEMENTS_STATE, SLIDES_MANAGER_STATE } from './constants';
import { calculateIndent, getLoopPoints, getTargetIndex } from './helpers';
import { useSlideAnimation } from './hooks';
import { ControlElementsState, SlidesManagerState } from './types';
import styles from '../BaseGallery.module.css';

const stylesBullets = {
dark: styles['BaseGallery__bullets--dark'],
light: styles['BaseGallery__bullets--light'],
};

const warn = warnOnce('Gallery');

export const CarouselBase = ({
Expand All @@ -39,6 +35,8 @@ export const CarouselBase = ({
showArrows,
getRef,
arrowSize = 'l',
bulletsClassName,
slideClassName,
...restProps
}: BaseGalleryProps): React.ReactNode => {
const slidesStore = React.useRef<Record<string, HTMLDivElement | null>>({});
Expand Down Expand Up @@ -337,7 +335,7 @@ export const CarouselBase = ({
<div className={styles['BaseGallery__layer']} ref={layerRef}>
{React.Children.map(children, (item: React.ReactNode, i: number) => (
<div
className={styles['BaseGallery__slide']}
className={classNames(styles['BaseGallery__slide'], slideClassName)}
key={`slide-${i}`}
ref={(el) => setSlideRef(el, i)}
>
Expand All @@ -348,20 +346,14 @@ export const CarouselBase = ({
</Touch>

{bullets && (
<div
aria-hidden
className={classNames(styles['BaseGallery__bullets'], stylesBullets[bullets])}
<GalleryBullets
bullets={bullets}
slideIndex={slideIndex}
onChange={onChange}
bulletsClassName={bulletsClassName}
>
{React.Children.map(children, (_item: React.ReactNode, index: number) => (
<div
className={classNames(
styles['BaseGallery__bullet'],
index === slideIndex && styles['BaseGallery__bullet--active'],
)}
key={index}
/>
))}
</div>
{children}
</GalleryBullets>
)}

{showArrows && hasPointer && canSlideLeft && (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as React from 'react';
import { classNames } from '@vkontakte/vkjs';
import { Tappable } from '../../Tappable/Tappable';
import { BaseGalleryProps } from '../types';
import styles from '../BaseGallery.module.css';

const stylesBullets = {
dark: styles['BaseGallery__bullets--dark'],
light: styles['BaseGallery__bullets--light'],
};

interface GalleryBulletsProps
extends Pick<BaseGalleryProps, 'slideIndex' | 'children' | 'onChange' | 'bulletsClassName'> {
bullets: Exclude<BaseGalleryProps['bullets'], false | undefined>;
}

export const GalleryBullets = ({
bullets,
slideIndex,
children,
onChange,
bulletsClassName,
}: GalleryBulletsProps) => {

Check warning on line 23 in packages/vkui/src/components/BaseGallery/GalleryBullets/GalleryBullets.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/BaseGallery/GalleryBullets/GalleryBullets.tsx#L23

Added line #L23 was not covered by tests
return (
<div
aria-hidden
className={classNames(
styles['BaseGallery__bullets'],
stylesBullets[bullets],
bulletsClassName,
)}
>
{React.Children.map(children, (_item: React.ReactNode, index: number) => (
<Tappable

Check warning on line 34 in packages/vkui/src/components/BaseGallery/GalleryBullets/GalleryBullets.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/BaseGallery/GalleryBullets/GalleryBullets.tsx#L34

Added line #L34 was not covered by tests
Component="button"
className={classNames(
styles['BaseGallery__bullet'],
index === slideIndex && styles['BaseGallery__bullet--active'],

Check warning on line 38 in packages/vkui/src/components/BaseGallery/GalleryBullets/GalleryBullets.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/BaseGallery/GalleryBullets/GalleryBullets.tsx#L38

Added line #L38 was not covered by tests
)}
onClick={() => onChange?.(index)}

Check warning on line 40 in packages/vkui/src/components/BaseGallery/GalleryBullets/GalleryBullets.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/BaseGallery/GalleryBullets/GalleryBullets.tsx#L40

Added line #L40 was not covered by tests
key={index}
/>
))}
</div>
);
};
2 changes: 2 additions & 0 deletions packages/vkui/src/components/BaseGallery/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export interface BaseGalleryProps
*/
onNextClick?: (event: React.MouseEvent) => void;
bullets?: 'dark' | 'light' | false;
bulletsClassName?: string;
slideClassName?: string;
/**
* Позволяет отключить реагирование на тач-события
*/
Expand Down