|
| 1 | +import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; |
| 2 | +import { __ } from '@wordpress/i18n'; |
| 3 | +import type { CarouselAttributes } from './types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * v2.0.0 save — before a11y announcement fields were added to context |
| 7 | + * and the live-region <span> was appended. |
| 8 | + * |
| 9 | + * @param {Object} root0 Component props. |
| 10 | + * @param {CarouselAttributes} root0.attributes Block attributes. |
| 11 | + */ |
| 12 | +function SaveV200( { |
| 13 | + attributes, |
| 14 | +}: { |
| 15 | + attributes: CarouselAttributes; |
| 16 | +} ) { |
| 17 | + const { |
| 18 | + loop, |
| 19 | + dragFree, |
| 20 | + carouselAlign, |
| 21 | + containScroll, |
| 22 | + direction, |
| 23 | + autoplay, |
| 24 | + autoplayDelay, |
| 25 | + autoplayStopOnInteraction, |
| 26 | + autoplayStopOnMouseEnter, |
| 27 | + ariaLabel, |
| 28 | + slideGap, |
| 29 | + axis, |
| 30 | + height, |
| 31 | + slidesToScroll, |
| 32 | + } = attributes; |
| 33 | + |
| 34 | + const context = { |
| 35 | + options: { |
| 36 | + loop, |
| 37 | + dragFree, |
| 38 | + align: carouselAlign, |
| 39 | + containScroll, |
| 40 | + direction, |
| 41 | + axis, |
| 42 | + slidesToScroll: slidesToScroll === 'auto' ? 'auto' : parseInt( slidesToScroll, 10 ), |
| 43 | + }, |
| 44 | + autoplay: autoplay |
| 45 | + ? { |
| 46 | + delay: autoplayDelay, |
| 47 | + stopOnInteraction: autoplayStopOnInteraction, |
| 48 | + stopOnMouseEnter: autoplayStopOnMouseEnter, |
| 49 | + } |
| 50 | + : false, |
| 51 | + isPlaying: !! autoplay, |
| 52 | + timerIterationId: 0, |
| 53 | + selectedIndex: -1, |
| 54 | + scrollSnaps: [] as { index: number }[], |
| 55 | + canScrollPrev: false, |
| 56 | + canScrollNext: false, |
| 57 | + scrollProgress: 0, |
| 58 | + slideCount: 0, |
| 59 | + /* translators: %d: slide number */ |
| 60 | + ariaLabelPattern: __( 'Go to slide %d', 'rt-carousel' ), |
| 61 | + }; |
| 62 | + |
| 63 | + const blockProps = useBlockProps.save( { |
| 64 | + className: 'rt-carousel', |
| 65 | + role: 'region', |
| 66 | + 'aria-roledescription': 'carousel', |
| 67 | + 'aria-label': ariaLabel, |
| 68 | + dir: direction, |
| 69 | + 'data-axis': axis, |
| 70 | + 'data-loop': loop ? 'true' : undefined, |
| 71 | + 'data-wp-interactive': 'rt-carousel/carousel', |
| 72 | + 'data-wp-context': JSON.stringify( context ), |
| 73 | + 'data-wp-init': 'callbacks.initCarousel', |
| 74 | + style: { |
| 75 | + '--rt-carousel-gap': `${ slideGap }px`, |
| 76 | + '--rt-carousel-height': axis === 'y' ? height : undefined, |
| 77 | + } as React.CSSProperties, |
| 78 | + } ); |
| 79 | + |
| 80 | + const innerBlocksProps = useInnerBlocksProps.save( blockProps ) as ReturnType< |
| 81 | + typeof useInnerBlocksProps.save |
| 82 | + > & { |
| 83 | + children: React.ReactNode; |
| 84 | + }; |
| 85 | + |
| 86 | + return <div { ...innerBlocksProps } />; |
| 87 | +} |
| 88 | + |
| 89 | +const deprecated = [ |
| 90 | + { |
| 91 | + attributes: { |
| 92 | + allowedSlideBlocks: { type: 'array' as const, default: [] }, |
| 93 | + loop: { type: 'boolean' as const, default: false }, |
| 94 | + dragFree: { type: 'boolean' as const, default: false }, |
| 95 | + carouselAlign: { type: 'string' as const, default: 'start' }, |
| 96 | + containScroll: { type: 'string' as const, default: 'trimSnaps' }, |
| 97 | + direction: { type: 'string' as const, default: 'ltr' }, |
| 98 | + axis: { type: 'string' as const, default: 'x' }, |
| 99 | + height: { type: 'string' as const, default: '300px' }, |
| 100 | + autoplay: { type: 'boolean' as const, default: false }, |
| 101 | + autoplayDelay: { type: 'number' as const, default: 4000 }, |
| 102 | + autoplayStopOnInteraction: { type: 'boolean' as const, default: true }, |
| 103 | + autoplayStopOnMouseEnter: { type: 'boolean' as const, default: false }, |
| 104 | + ariaLabel: { type: 'string' as const, default: 'Carousel' }, |
| 105 | + slideGap: { type: 'number' as const, default: 0 }, |
| 106 | + slidesToScroll: { type: 'string' as const, default: '1' }, |
| 107 | + }, |
| 108 | + supports: { |
| 109 | + interactivity: true, |
| 110 | + align: [ 'wide', 'full' ], |
| 111 | + html: false, |
| 112 | + color: { |
| 113 | + text: false, |
| 114 | + background: true, |
| 115 | + }, |
| 116 | + }, |
| 117 | + save: SaveV200, |
| 118 | + }, |
| 119 | +]; |
| 120 | + |
| 121 | +export default deprecated; |
0 commit comments