A drawer component for React.
Revert back to initial height after visual viewport has been changed (#134)
Emil Kowalskib24e6a6c5e6a
diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.ymldeleted file mode 100644index 37d206d..0000000--- a/.github/workflows/playwright.yml+++ /dev/null@@ -1,27 +0,0 @@-name: Playwright Tests-on:- push:- branches: [ main, master ]- pull_request:- branches: [ main, master ]-jobs:- test:- timeout-minutes: 60- runs-on: ubuntu-latest- steps:- - uses: actions/checkout@v3- - uses: actions/setup-node@v3- with:- node-version: 18- - name: Install dependencies- run: pnpm install- - name: Install Playwright Browsers- run: pnpm exec playwright install --with-deps- - name: Run Playwright tests- run: pnpm exec playwright test- - uses: actions/upload-artifact@v3- if: always()- with:- name: playwright-report- path: playwright-report/- retention-days: 30diff --git a/package.json b/package.jsonindex d7127b9..6319ef5 100644--- a/package.json+++ b/package.json@@ -37,6 +37,7 @@"@radix-ui/react-dialog": "^1.0.4","eslint": "^7.32.0","prettier": "^2.5.1",+ "typescript": "5.2.2","tsup": "^6.4.0","turbo": "1.6"},diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex a6ab970..cb4854c 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -10,14 +10,16 @@ importers:prettier: ^2.5.1tsup: ^6.4.0turbo: '1.6'+ typescript: 5.2.2dependencies:'@radix-ui/react-dialog': 1.0.4devDependencies:'@playwright/test': 1.37.1eslint: 7.32.0prettier: 2.8.8- tsup: 6.7.0+ tsup: [email protected]turbo: 1.6.3+ typescript: 5.2.2test:specifiers:@@ -67,7 +69,7 @@ importers:react-dom: 18.2.0react-merge-refs: ^2.0.2tailwindcss: 3.3.3- typescript: 5.1.6+ typescript: 5.2.2vaul: workspace:*dependencies:'@types/node': 20.4.2@@ -77,14 +79,14 @@ importers:autoprefixer: [email protected]clsx: 2.0.0eslint: 8.45.0- eslint-config-next: [email protected][email protected]+ eslint-config-next: [email protected][email protected]postcss: 8.4.26react: 18.2.0react-dom: [email protected]react-merge-refs: 2.0.2tailwindcss: 3.3.3- typescript: 5.1.6+ typescript: 5.2.2vaul: link:..packages:@@ -990,7 +992,7 @@ packages:resolution: {integrity: sha512-5cJ8CB4yAx7BH1oMvdU0Jh9lrEXyPkar6F9G/ERswkCuvP4KQZfZkSjcMbAICCpQTN4OuZn8tz0HiKv9TGZgrQ==}dev: false- /@typescript-eslint/parser/[email protected][email protected]:+ /@typescript-eslint/parser/[email protected][email protected]:resolution: {integrity: sha512-uq0sKyw6ao1iFOZZGk9F8Nro/8+gfB5ezl1cA06SrqbgJAt0SRoFhb9pXaHvkrxUpZaoLxt8KlovHNk8Gp6/HQ==}engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}peerDependencies:@@ -1002,10 +1004,10 @@ packages:dependencies:'@typescript-eslint/scope-manager': 5.59.2'@typescript-eslint/types': 5.59.2- '@typescript-eslint/typescript-estree': [email protected]+ '@typescript-eslint/typescript-estree': [email protected]debug: 4.3.4eslint: 8.45.0- typescript: 5.1.6+ typescript: 5.2.2transitivePeerDependencies:- supports-colordev: false@@ -1043,27 +1045,6 @@ packages:engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}dev: false- /@typescript-eslint/typescript-estree/[email protected]:- resolution: {integrity: sha512-+j4SmbwVmZsQ9jEyBMgpuBD0rKwi9RxRpjX71Brr73RsYnEr3Lt5QZ624Bxphp8HUkSKfqGnPJp1kA5nl0Sh7Q==}- engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}- peerDependencies:- typescript: '*'- peerDependenciesMeta:- typescript:- optional: true- dependencies:- '@typescript-eslint/types': 5.59.2- '@typescript-eslint/visitor-keys': 5.59.2- debug: 4.3.4- globby: 11.1.0- is-glob: 4.0.3- semver: 7.5.0- tsutils: [email protected]- typescript: 5.1.6- transitivePeerDependencies:- - supports-color- dev: false-/@typescript-eslint/typescript-estree/[email protected]:resolution: {integrity: sha512-+j4SmbwVmZsQ9jEyBMgpuBD0rKwi9RxRpjX71Brr73RsYnEr3Lt5QZ624Bxphp8HUkSKfqGnPJp1kA5nl0Sh7Q==}engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}@@ -1752,7 +1733,7 @@ packages:resolution: {integrity: sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==}engines: {node: '>=10'}- /eslint-config-next/[email protected][email protected]:+ /eslint-config-next/[email protected][email protected]:resolution: {integrity: sha512-+JjcM6lQmFR5Mw0ORm9o1CR29+z/uajgSfYAPEGIBxOhTHBgCMs7ysuwi72o7LkMmA8E3N7/h09pSGZxs0s85g==}peerDependencies:eslint: ^7.23.0 || ^8.0.0@@ -1763,7 +1744,7 @@ packages:dependencies:'@next/eslint-plugin-next': 13.4.10'@rushstack/eslint-patch': 1.2.0- '@typescript-eslint/parser': [email protected][email protected]+ '@typescript-eslint/parser': [email protected][email protected]eslint: 8.45.0eslint-import-resolver-node: 0.3.7eslint-import-resolver-typescript: 3.5.5_59236bfd07d3fab2f40480f5c7b98a36@@ -1771,7 +1752,7 @@ packages:eslint-plugin-jsx-a11y: [email protected]eslint-plugin-react: [email protected]eslint-plugin-react-hooks: [email protected]- typescript: 5.1.6+ typescript: 5.2.2transitivePeerDependencies:- supports-colordev: false@@ -3904,7 +3885,7 @@ packages:resolution: {integrity: sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==}dev: false- /tsup/6.7.0:+ /tsup/[email protected]:resolution: {integrity: sha512-L3o8hGkaHnu5TdJns+mCqFsDBo83bJ44rlK7e6VdanIvpea4ArPcU3swWGsLVbXak1PqQx/V+SSmFPujBK+zEQ==}engines: {node: '>=14.18'}hasBin: true@@ -3934,21 +3915,12 @@ packages:source-map: 0.8.0-beta.0sucrase: 3.33.0tree-kill: 1.2.2+ typescript: 5.2.2transitivePeerDependencies:- supports-color- ts-nodedev: true- /tsutils/[email protected]:- resolution: {integrity: sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==}- engines: {node: '>= 6'}- peerDependencies:- typescript: '>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta'- dependencies:- tslib: 1.14.1- typescript: 5.1.6- dev: false-/tsutils/[email protected]:resolution: {integrity: sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==}engines: {node: '>= 6'}@@ -4038,17 +4010,10 @@ packages:is-typed-array: 1.1.10dev: false- /typescript/5.1.6:- resolution: {integrity: sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==}- engines: {node: '>=14.17'}- hasBin: true- dev: false-/typescript/5.2.2:resolution: {integrity: sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==}engines: {node: '>=14.17'}hasBin: true- dev: false/unbox-primitive/1.0.2:resolution: {integrity: sha512-61pPlCD9h51VoreyJ0BReideM3MDKMKnh6+V9L08331ipq6Q8OFXZYiqP6n/tbHx4s5I9uRhcye6BrbkizkBDw==}diff --git a/src/index.tsx b/src/index.tsxindex 08db008..1f6e421 100644--- a/src/index.tsx+++ b/src/index.tsx@@ -1,7 +1,7 @@'use client';import * as DialogPrimitive from '@radix-ui/react-dialog';-import React, { useCallback } from 'react';+import React from 'react';import { DrawerContext, useDrawerContext } from './context';import './style.css';import { usePreventScroll, isInput, isIOS } from './use-prevent-scroll';@@ -87,8 +87,9 @@ function Root({const previousDiffFromInitial = React.useRef(0);const drawerRef = React.useRef<HTMLDivElement>(null);const drawerHeightRef = React.useRef(drawerRef.current?.getBoundingClientRect().height || 0);+ const initialDrawerHeight = React.useRef(0);- const onSnapPointChange = useCallback((activeSnapPointIndex: number) => {+ const onSnapPointChange = React.useCallback((activeSnapPointIndex: number) => {// Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();}, []);@@ -307,6 +308,9 @@ function Root({// This is the height of the keyboardlet diffFromInitial = window.innerHeight - visualViewportHeight;const drawerHeight = drawerRef.current.getBoundingClientRect().height || 0;+ if (!initialDrawerHeight.current) {+ initialDrawerHeight.current = drawerHeight;+ }const offsetFromTop = drawerRef.current.getBoundingClientRect().top;// visualViewport height may change due to some subtle changes to the keyboard. Checking if the height changed by 60 or more will make sure that they keyboard really changed its open state.@@ -335,7 +339,7 @@ function Root({drawerRef.current.style.height = `${Math.max(newDrawerHeight, visualViewportHeight - offsetFromTop)}px`;}} else {- drawerRef.current.style.height = 'initial';+ drawerRef.current.style.height = `${initialDrawerHeight.current}px`;}if (snapPoints && snapPoints.length > 0 && !keyboardIsOpen.current) {@@ -727,9 +731,9 @@ const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (onPointerUp={onRelease}ref={composedRef}style={- snapPointsOffset+ snapPointsOffset && snapPointsOffset.length > 0? ({- '--snap-point-height': `${snapPointsOffset[0]}px`,+ '--snap-point-height': `${snapPointsOffset[0]!}px`,...style,} as React.CSSProperties): stylediff --git a/test/tests/nested.spec.ts b/test/tests/nested.spec.tsindex 9edf534..f9b1ac3 100644--- a/test/tests/nested.spec.ts+++ b/test/tests/nested.spec.ts@@ -6,7 +6,7 @@ test.beforeEach(async ({ page }) => {await page.goto('/nested-drawers');});-test.describe.only('Nested tests', () => {+test.describe('Nested tests', () => {test('should open and close nested drawer', async ({ page }) => {await openDrawer(page);await page.getByTestId('nested-trigger').click();diff --git a/test/tests/with-snap-points.test.ts b/test/tests/with-snap-points.test.tsdeleted file mode 100644index 554cf27..0000000--- a/test/tests/with-snap-points.test.ts+++ /dev/null@@ -1,20 +0,0 @@-// import { test, expect } from '@playwright/test';-// import { ANIMATION_DURATION } from './constants';-// import { openDrawer } from './helpers';--// test.beforeEach(async ({ page }) => {-// await page.goto('/with-snap-points');-// });--// test.describe.only('Base tests', () => {-// test('should change active snap point', async ({ page }) => {-// await openDrawer(page);-// await page.hover('[vaul-drawer]');-// await page.mouse.down();-// await page.mouse.move(0, -800);-// await page.mouse.up();-// const activeSnap = await page.getByTestId('active-snap-index').innerText();-// await page.waitForTimeout(ANIMATION_DURATION);-// expect(activeSnap).toBe('2');-// });-// });diff --git a/test/tsconfig.json b/test/tsconfig.jsonindex e59724b..3b5697a 100644--- a/test/tsconfig.json+++ b/test/tsconfig.json@@ -1,6 +1,7 @@{"compilerOptions": {"target": "es5",+ "baseUrl": ".","lib": ["dom", "dom.iterable", "esnext"],"allowJs": true,"skipLibCheck": true,diff --git a/website/package.json b/website/package.jsonindex 372db0e..0b7ea6a 100644--- a/website/package.json+++ b/website/package.json@@ -23,7 +23,7 @@"react-dom": "18.2.0","react-merge-refs": "^2.0.2","tailwindcss": "3.3.3",- "typescript": "5.1.6",+ "typescript": "5.2.2","vaul": "workspace:*"}}