feat(DTGallery): add optional activeIndex prop for external control
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -41,6 +41,9 @@ export type DTGalleryItem =
|
|||||||
|
|
||||||
interface DTGalleryProps {
|
interface DTGalleryProps {
|
||||||
items: DTGalleryItem[];
|
items: DTGalleryItem[];
|
||||||
|
/** Externally-controlled active index. When this value changes, the gallery
|
||||||
|
* jumps to the corresponding item. Thumbnail clicks still work normally. */
|
||||||
|
activeIndex?: number;
|
||||||
/** Color variant @default 'normal' */
|
/** Color variant @default 'normal' */
|
||||||
variant?: DTVariant;
|
variant?: DTVariant;
|
||||||
/** Aspect ratio (width / height) for the display area @default 1 */
|
/** Aspect ratio (width / height) for the display area @default 1 */
|
||||||
@@ -85,12 +88,20 @@ const fillStyle: CSSProperties = {
|
|||||||
|
|
||||||
export function DTGallery({
|
export function DTGallery({
|
||||||
items,
|
items,
|
||||||
|
activeIndex: activeIndexProp,
|
||||||
variant = 'normal',
|
variant = 'normal',
|
||||||
aspectRatio = 1,
|
aspectRatio = 1,
|
||||||
className,
|
className,
|
||||||
style,
|
style,
|
||||||
}: DTGalleryProps) {
|
}: DTGalleryProps) {
|
||||||
const [activeIndex, setActiveIndex] = useState(0);
|
const [activeIndex, setActiveIndex] = useState(activeIndexProp ?? 0);
|
||||||
|
|
||||||
|
// Sync external activeIndex prop to internal state when it changes
|
||||||
|
useEffect(() => {
|
||||||
|
if (activeIndexProp != null && activeIndexProp >= 0 && activeIndexProp < items.length) {
|
||||||
|
setActiveIndex(activeIndexProp);
|
||||||
|
}
|
||||||
|
}, [activeIndexProp, items.length]);
|
||||||
|
|
||||||
const hasModel = items.some((i) => i.type === 'model3d');
|
const hasModel = items.some((i) => i.type === 'model3d');
|
||||||
useModelViewerScript(hasModel);
|
useModelViewerScript(hasModel);
|
||||||
|
|||||||
Reference in New Issue
Block a user