Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Remove Safari theme animation for now (#119)

Emil Kowalski42a80213dec3
diff --git a/src/context.ts b/src/context.ts
index 8e7d430..dd14e08 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -13,7 +13,6 @@ interface DrawerContextValue {
dismissible: boolean;
isOpen: boolean;
keyboardIsOpen: React.MutableRefObject<boolean>;
- experimentalSafariThemeAnimation?: boolean;
snapPointsOffset: number[] | null;
snapPoints?: (number | string)[] | null;
modal: boolean;
@@ -38,7 +37,6 @@ export const DrawerContext = React.createContext<DrawerContextValue>({
dismissible: false,
isOpen: false,
keyboardIsOpen: { current: false },
- experimentalSafariThemeAnimation: false,
snapPointsOffset: null,
snapPoints: null,
modal: false,
diff --git a/src/index.tsx b/src/index.tsx
index f4bd723..a4113ed 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -6,7 +6,6 @@ import { DrawerContext, useDrawerContext } from './context';
import './style.css';
import { usePreventScroll, isInput, isIOS } from './use-prevent-scroll';
import { useComposedRefs } from './use-composed-refs';
-import { useSafariThemeColor } from './use-safari-theme-color';
import { usePositionFixed } from './use-position-fixed';
import { useSnapPoints } from './use-snap-points';
import { set, reset, getTranslateY, dampenValue } from './helpers';
@@ -45,7 +44,6 @@ type DialogProps = {
dismissible?: boolean;
onDrag?: (event: React.PointerEvent<HTMLDivElement>, percentageDragged: number) => void;
onRelease?: (event: React.PointerEvent<HTMLDivElement>, open: boolean) => void;
- experimentalSafariThemeAnimation?: boolean;
modal?: boolean;
nested?: boolean;
onClose?: () => void;
@@ -58,7 +56,6 @@ function Root({
shouldScaleBackground,
onDrag: onDragProp,
onRelease: onReleaseProp,
- experimentalSafariThemeAnimation,
snapPoints,
nested,
closeThreshold = CLOSE_THRESHOLD,
@@ -89,12 +86,6 @@ function Root({
const previousDiffFromInitial = React.useRef(0);
const drawerRef = React.useRef<HTMLDivElement>(null);
const drawerHeightRef = React.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
- const { onDrag: changeThemeColorOnDrag, onRelease: themeTransitionOnRelease } = useSafariThemeColor(
- drawerRef,
- overlayRef,
- isOpen,
- experimentalSafariThemeAnimation,
- );
const onSnapPointChange = useCallback((activeSnapPointIndex: number) => {
// Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
@@ -258,7 +249,6 @@ function Root({
const opacityValue = 1 - percentageDragged;
if (shouldFade || (fadeFromIndex && activeSnapPointIndex === fadeFromIndex - 1)) {
- changeThemeColorOnDrag(percentageDragged);
onDragProp?.(event, percentageDragged);
set(
@@ -379,8 +369,8 @@ function Root({
}
setTimeout(() => {
- setIsOpen(false);
setVisible(false);
+ setIsOpen(false);
}, 300);
setTimeout(() => {
@@ -498,14 +488,12 @@ function Root({
if (distMoved > 0) {
resetDrawer();
onReleaseProp?.(event, true);
- themeTransitionOnRelease(true);
return;
}
if (velocity > VELOCITY_THRESHOLD) {
closeDrawer();
onReleaseProp?.(event, false);
- themeTransitionOnRelease(false);
return;
}
@@ -514,12 +502,10 @@ function Root({
if (swipeAmount >= visibleDrawerHeight * closeThreshold) {
closeDrawer();
onReleaseProp?.(event, false);
- themeTransitionOnRelease(false);
return;
}
onReleaseProp?.(event, true);
- themeTransitionOnRelease(true);
resetDrawer();
}
@@ -649,7 +635,6 @@ function Root({
keyboardIsOpen,
modal,
snapPointsOffset,
- experimentalSafariThemeAnimation,
}}
>
{children}
@@ -660,8 +645,7 @@ function Root({
const Overlay = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>>(
function ({ children, style, ...rest }, ref) {
- const { overlayRef, snapPoints, onRelease, experimentalSafariThemeAnimation, shouldFade, isOpen, visible } =
- useDrawerContext();
+ const { overlayRef, snapPoints, onRelease, shouldFade, isOpen, visible } = useDrawerContext();
const composedRef = useComposedRefs(ref, overlayRef);
const hasSnapPoints = snapPoints && snapPoints.length > 0;
@@ -673,7 +657,6 @@ const Overlay = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<
vaul-overlay=""
vaul-snap-points={isOpen && hasSnapPoints ? 'true' : 'false'}
vaul-snap-points-overlay={isOpen && shouldFade ? 'true' : 'false'}
- vaul-theme-transition={experimentalSafariThemeAnimation ? 'true' : 'false'}
{...rest}
/>
);
diff --git a/src/style.css b/src/style.css
index 999053a..1adee65 100644
--- a/src/style.css
+++ b/src/style.css
@@ -44,24 +44,6 @@
}
}
-@keyframes show-theme-overlay {
- from {
- background: var(--vaul-overlay-background-start);
- }
- to {
- background: var(--vaul-overlay-background-end);
- }
-}
-
-@keyframes hide-theme-overlay {
- from {
- background: var(--vaul-overlay-background-end);
- }
- to {
- background: var(--vaul-overlay-background-start);
- }
-}
-
@media (hover: hover) and (pointer: fine) {
[vaul-drawer] {
user-select: none;
diff --git a/src/use-safari-theme-color.ts b/src/use-safari-theme-color.ts
deleted file mode 100644
index 502960c..0000000
--- a/src/use-safari-theme-color.ts
+++ /dev/null
@@ -1,207 +0,0 @@
-import React, { useCallback } from 'react';
-import { bezier } from './bezier-easing';
-import { isIOS, isSafari } from './use-prevent-scroll';
-
-const bezierEasing = bezier(0.32, 0.72, 0, 1);
-
-type RGB = [number, number, number];
-
-function extractRGBA(str: string): [number, number, number, number] {
- const match = str.match(/rgba?\((\d+),\s*(\d+),\s*(\d+),\s*(\d*(?:\.\d+)?)\)/);
- if (!match) throw new Error('Invalid color string');
- return [Number(match[1]), Number(match[2]), Number(match[3]), Number(match[4])];
-}
-
-// If the overlay is rgba(0, 0, 0, 0.4), we can't just use this value as the theme-color, as it will be pure black instead of grey-ish.
-// Instead, we assume the background is white (or black depending on the theme) and calculate the result of putting the overlay color on top of it.
-function getNonTrasparentOverlayColor(rgbaStr: string, background: RGB): RGB {
- const [r, g, b, a] = extractRGBA(rgbaStr);
-
- const rgb: RGB = [
- Math.round(a * r + (1 - a) * Number(background[0])),
- Math.round(a * g + (1 - a) * Number(background[1])),
- Math.round(a * b + (1 - a) * Number(background[2])),
- ];
-
- return rgb;
-}
-
-function easing(t: number): number {
- return bezierEasing(t);
-}
-
-function interpolateColor(color1: number[], color2: number[], factor: number, linear: boolean) {
- if (arguments.length < 3) {
- factor = 0.5;
- }
- let result = color1.slice();
- for (let i = 0; i < 3; i++) {
- const delta = color2[i] - color1[i];
- const newColorComponent = linear ? color1[i] + factor * delta : color1[i] + easing(factor) * delta;
- result[i] = Math.round(newColorComponent);
- if (result[i] < 0) result[i] = 0;
- if (result[i] > 255) result[i] = 255;
- }
- return result;
-}
-
-function interpolateColors(color1: number[], color2: number[], steps: number, linear?: boolean): number[][] {
- let stepFactor = 1 / (steps - 1),
- interpolatedColorArray = [];
-
- for (let i = 0; i < steps; i++) {
- interpolatedColorArray.push(interpolateColor(color1, color2, stepFactor * i, Boolean(linear)));
- }
-
- return interpolatedColorArray;
-}
-export function useSafariThemeColor(
- drawer: React.RefObject<HTMLDivElement>,
- overlay: React.RefObject<HTMLDivElement>,
- isOpen: boolean,
- shouldAnimate?: boolean,
-) {
- const [backgroundColor, setBackgroundColor] = React.useState<RGB | null>([255, 255, 255]);
- const [overlayColor, setOverlayColor] = React.useState<RGB | null>([153, 153, 153]);
- const [releaseExit, setReleaseExit] = React.useState<boolean>(false);
- const [initialMetaThemeColor, setInitialMetaThemeColor] = React.useState<string | null>(null);
- const [metaElement, setMetaElement] = React.useState<HTMLMetaElement | null>(null);
- const shouldRun = React.useMemo(() => isIOS() && isSafari() && shouldAnimate, [shouldAnimate]);
-
- const interpolatedColorsEnter = React.useMemo(
- () => (backgroundColor && overlayColor ? interpolateColors(backgroundColor, overlayColor, 50) : null),
- [overlayColor, backgroundColor],
- );
-
- const interpolatedColorsExit = React.useMemo(
- () => (backgroundColor && overlayColor ? interpolateColors(overlayColor, backgroundColor, 50) : null),
- [overlayColor, backgroundColor],
- );
-
- const interpolatedColorsLinear = React.useMemo(
- () => (backgroundColor && overlayColor ? interpolateColors(overlayColor, backgroundColor, 50, true) : null),
- [overlayColor, backgroundColor],
- );
-
- React.useEffect(() => {
- if (!shouldRun) return;
- const documentElementStyle = getComputedStyle(document.documentElement);
- const backgroundColor = documentElementStyle
- .getPropertyValue('--vaul-overlay-background')
- .split(',')
- .map((c) => Number(c));
-
- const overlayColor = documentElementStyle.getPropertyValue('--vaul-overlay-background-end');
-
- const nonTransparentOverlayColor = getNonTrasparentOverlayColor(overlayColor, backgroundColor as RGB);
-
- setBackgroundColor(backgroundColor as RGB);
- setOverlayColor(nonTransparentOverlayColor);
- }, [shouldRun]);
-
- React.useEffect(() => {
- if (!shouldRun) return;
-
- if (!initialMetaThemeColor) {
- let metaThemeColor: HTMLMetaElement | null = document.querySelector('meta[name="theme-color"]');
-
- if (metaThemeColor) {
- setInitialMetaThemeColor(metaThemeColor.getAttribute('content'));
- } else {
- metaThemeColor = document.createElement('meta');
- metaThemeColor.name = 'theme-color';
- document.getElementsByTagName('head')[0].appendChild(metaThemeColor);
- }
- setMetaElement(metaThemeColor);
- }
- }, [initialMetaThemeColor, shouldRun]);
-
- const animate = useCallback(
- (colors: number[][]): number => {
- let start: number;
- let frameId: number;
-
- function draw(timeStamp: number) {
- if (!start) start = timeStamp;
- const elapsed = timeStamp - start;
- // dividing by 10 will give us a total time of 500.
- const index = Math.floor(elapsed / 10);
-
- if (overlay.current && colors && !releaseExit && drawer.current) {
- if (
- drawer.current.style.transform === 'translateY(0px)' &&
- drawer.current.getAttribute('vaul-clicked-outside') !== 'true'
- ) {
- // It's resetting, so don't apply these styles
- return;
- }
-
- if (index < colors.length) {
- const currentColor = colors[index];
- metaElement?.setAttribute('content', `rgb(${currentColor.join(',')})`);
-
- if (index === colors.length - 1 && initialMetaThemeColor && !isOpen) {
- metaElement?.setAttribute('content', initialMetaThemeColor as string);
- }
-
- frameId = requestAnimationFrame(draw);
- }
- }
- }
-
- frameId = requestAnimationFrame(draw);
- return frameId;
- },
- [drawer, isOpen, metaElement, releaseExit, initialMetaThemeColor, overlay],
- );
-
- React.useEffect(() => {
- if (!shouldRun || !interpolatedColorsEnter || !interpolatedColorsExit) return;
-
- const frameId = animate(isOpen ? interpolatedColorsEnter : interpolatedColorsExit);
-
- if (isOpen) {
- setReleaseExit(false);
- }
-
- return () => {
- if (frameId) cancelAnimationFrame(frameId);
- };
- }, [isOpen, shouldRun, animate, interpolatedColorsEnter, interpolatedColorsExit]);
-
- function onDrag(percentageDragged: number) {
- const metaThemeColor = document.querySelector('meta[name="theme-color"]');
- if (!shouldRun || !metaThemeColor || !interpolatedColorsLinear) return;
-
- // Calculate the index of the color array by mapping the proportion to the array length
- let colorIndex = Math.floor(percentageDragged * interpolatedColorsLinear.length);
-
- // Ensure colorIndex is between 0 and array length - 1
- colorIndex = Math.max(0, Math.min(interpolatedColorsLinear.length - 1, colorIndex));
-
- // Get the color
- const color = interpolatedColorsLinear[colorIndex];
-
- // Set the meta theme color
- metaThemeColor.setAttribute('content', `rgb(${color.join(',')})`);
- }
-
- function onRelease(isOpen: boolean) {
- const metaThemeColor = document.querySelector('meta[name="theme-color"]');
- if (!metaThemeColor || !shouldRun) return;
- setReleaseExit(true);
-
- // Get the current meta theme color and create color steps from it to overlayColor with non-linear interpolation to ensure same easing as overlay.
- const rgbValues = metaThemeColor.getAttribute('content').match(/\d+/g).map(Number);
-
- let colorSteps = interpolateColors(rgbValues, overlayColor as RGB, 50);
-
- if (!isOpen && backgroundColor) {
- colorSteps = interpolateColors(rgbValues, backgroundColor, 50);
- }
-
- animate(colorSteps);
- }
-
- return { onDrag, onRelease };
-}