Skip to content
Merged
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
58 changes: 58 additions & 0 deletions packages/core/ui/animation/index.ios.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
import { Animation } from './index.ios';
import { ios as iosBackground } from '../styling/background';

function shadowLayer() {
return { shadowPath: 'old-shadow', mask: null, addAnimationForKey: vi.fn() };
}

function animateSize(boxShadows: any[], layers: any[]) {
const view = { style: { backgroundInternal: { getBoxShadows: () => boxShadows } } };
const nativeView = {
layer: { mask: null, cornerRadius: 0 },
hasNonUniformBorder: false,
outerShadowContainerLayer: { mask: null, sublayers: Object.assign([...layers], { count: layers.length }) },
};
const bounds = { origin: { x: 0, y: 0 }, size: { width: 100, height: 50 } };

(Animation as any).animateNestedLayerSizeUsingBasicAnimation(nativeView, bounds, { target: view }, {}, {});

return { view, bounds };
}

describe('iOS Animation size with box shadows', () => {
beforeAll(() => {
vi.stubGlobal('CAShapeLayer', class {});
});

afterEach(() => {
vi.restoreAllMocks();
});

it('animates each shadow layer to the paths of its own box shadow', () => {
vi.spyOn(Animation as any, '_createBasicAnimation').mockImplementation((args: any) => args);
const generate = vi.spyOn(iosBackground, 'generateShadowLayerPaths').mockImplementation((_view, boxShadow: any) => ({ maskPath: `mask-${boxShadow.id}`, shadowPath: `shadow-${boxShadow.id}` }));
const boxShadows = [{ id: 'a' }, { id: 'b' }];
const layers = [shadowLayer(), shadowLayer()];

const { view, bounds } = animateSize(boxShadows, layers);

expect(generate.mock.calls).toEqual([
[view, boxShadows[0], bounds],
[view, boxShadows[1], bounds],
]);
expect(layers[0].addAnimationForKey.mock.calls[0][0].toValue).toBe('shadow-a');
expect(layers[1].addAnimationForKey.mock.calls[0][0].toValue).toBe('shadow-b');
});

it('skips shadow layers that have no matching box shadow', () => {
vi.spyOn(Animation as any, '_createBasicAnimation').mockImplementation((args: any) => args);
const generate = vi.spyOn(iosBackground, 'generateShadowLayerPaths').mockImplementation(() => ({ maskPath: null, shadowPath: null }));
const layers = [shadowLayer(), shadowLayer()];

animateSize([{ id: 'a' }], layers);

expect(generate).toHaveBeenCalledTimes(1);
expect(layers[1].addAnimationForKey).not.toHaveBeenCalled();
});
});
8 changes: 7 additions & 1 deletion packages/core/ui/animation/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,10 +894,16 @@ export class Animation extends AnimationBase {

const outerShadowLayers = nativeView.outerShadowContainerLayer.sublayers;
if (outerShadowLayers?.count) {
const { maskPath, shadowPath } = iosBackground.generateShadowLayerPaths(view, bounds);
const boxShadows = view.style.backgroundInternal.getBoxShadows();

for (let i = 0, count = outerShadowLayers.count; i < count; i++) {
const boxShadow = boxShadows?.[i];
if (!boxShadow) {
continue;
}

const shadowLayer = outerShadowLayers[i];
const { maskPath, shadowPath } = iosBackground.generateShadowLayerPaths(view, boxShadow, bounds);

shadowLayer.addAnimationForKey(
this._createBasicAnimation(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/ui/styling/background.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export namespace ios {
export function clearBackgroundVisualEffects(view: View): void;
export function createUIImageFromURI(view: View, imageURI: string, flip: boolean, callback: (image: any) => void): void;
export function generateClipPath(view: View, bounds: any /* CGRect */): any;
export function generateShadowLayerPaths(view: View, bounds: any /* CGRect */): { maskPath: any; shadowPath: any };
export function generateShadowLayerPaths(view: View, boxShadow: BoxShadow, bounds: any /* CGRect */): { maskPath: any; shadowPath: any };
export function getUniformBorderRadius(view: View, bounds: any /* CGRect */): number;
export function generateNonUniformBorderInnerClipRoundedPath(view: View, bounds: any /* CGRect */): any;
export function generateNonUniformBorderOuterClipRoundedPath(view: View, bounds: any /* CGRect */): any;
Expand Down
Loading