Skip to content

Commit

Permalink
Merge pull request #34 from readyplayerme/fix/SRV-305-fix-issues-with…
Browse files Browse the repository at this point in the history
…-fit-function-not-working

fix(asset): refresh fit, when the model is loaded
  • Loading branch information
rk132 authored Jun 7, 2023
2 parents 909568b + 8ebe95a commit cc357a0
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/components/Models/BoundsModel/BoundsModel.container.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, ReactNode, useEffect, useState, cloneElement, useMemo, ReactElement } from 'react';
import React, { FC, ReactNode, useEffect, useState, cloneElement, useMemo, ReactElement, useCallback } from 'react';
import { useBounds } from '@react-three/drei';
import { BaseModelProps } from 'src/types';

Expand All @@ -12,10 +12,18 @@ export const BoundsModelContainer: FC<BoundsModelContainerProps> = ({ modelSrc,
const bounds = useBounds();
const [fallback, setFallback] = useState<JSX.Element>(<></>);

const onChildLoaded = useCallback(() => {
if (fit) {
bounds.refresh().clip().fit();
}
}, [bounds, fit]);

const childModel = useMemo(
() =>
React.Children.map(children, (child) => cloneElement(child as ReactElement, { setModelFallback: setFallback })),
[modelSrc, children]
React.Children.map(children, (child) =>
cloneElement(child as ReactElement, { setModelFallback: setFallback, onLoaded: onChildLoaded })
),
[modelSrc, children, onChildLoaded]
);

useEffect(() => {
Expand Down

0 comments on commit cc357a0

Please sign in to comment.