Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(PageLayout): use dvh to support the correct height on iOS devices #2251

Merged
merged 7 commits into from
Oct 3, 2022
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
5 changes: 5 additions & 0 deletions .changeset/few-spoons-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Add support for the dvh unit in `PageLayout` in order to correctly display pane contents on iOS devices
8 changes: 4 additions & 4 deletions examples/nextjs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ module.exports = {
testEnvironment: 'jsdom',
cacheDirectory: '.test',
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/stories/**', '!**/*.stories.{js,jsx,ts,tsx}'],
setupFilesAfterEnv: [
'<rootDir>/src/utils/test-matchers.tsx',
'<rootDir>/src/utils/test-deprecations.tsx',
'<rootDir>/src/utils/test-helpers.tsx'
],
setupFiles: ['<rootDir>/src/utils/test-helpers.tsx'],
setupFilesAfterEnv: ['<rootDir>/src/utils/test-matchers.tsx', '<rootDir>/src/utils/test-deprecations.tsx'],
testMatch: ['<rootDir>/(src|codemods)/**/*.test.[jt]s?(x)', '!**/*.types.test.[jt]s?(x)'],
transformIgnorePatterns: [
'node_modules/(?!@github/combobox-nav|@koddsson/textarea-caret|@github/markdown-toolbar-element)'
Expand Down
27 changes: 25 additions & 2 deletions src/PageLayout/useStickyPaneHeight.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import {useInView} from 'react-intersection-observer'
import {canUseDOM} from '../utils/environment'

/**
* Calculates the height of the sticky pane such that it always
Expand All @@ -9,7 +10,7 @@ export function useStickyPaneHeight() {
const rootRef = React.useRef<HTMLDivElement>(null)

// Default the height to the viewport height
const [height, setHeight] = React.useState('100vh')
const [height, setHeight] = React.useState(dvh(100))
const [offsetHeader, setOffsetHeader] = React.useState<number | string>(0)

// Create intersection observers to track the top and bottom of the content region
Expand Down Expand Up @@ -47,7 +48,9 @@ export function useStickyPaneHeight() {
// We need to account for this when calculating the offset.
const overflowScroll = Math.max(window.scrollY + window.innerHeight - document.body.scrollHeight, 0)

calculatedHeight = `calc(100vh - (max(${topOffset}px, ${offsetHeaderWithUnits}) + ${bottomOffset}px - ${overflowScroll}px))`
calculatedHeight = `calc(${dvh(
100
)} - (max(${topOffset}px, ${offsetHeaderWithUnits}) + ${bottomOffset}px - ${overflowScroll}px))`
}

setHeight(calculatedHeight)
Expand Down Expand Up @@ -131,3 +134,23 @@ function isScrollable(element: Element) {

return hasScrollableContent && !isOverflowHidden
}

// TODO: there is currently an issue with dvh on Desktop Safari 15.6, 16.0. To
// work around it, we check to see if the device supports touch along with the
// dvh unit in order to target iPad. When the bug is addressed this check will
// no longer be needed
//
// @see https://bugs.webkit.org/show_bug.cgi?id=242758
const supportsTouchCallout = canUseDOM ? CSS.supports('-webkit-touch-callout', 'none') : false
const supportsDVH = canUseDOM ? CSS.supports('max-height', '100dvh') && supportsTouchCallout : false

/**
* Convert the given value to a dvh value, if supported, otherwise it falls back
* to vh
*/
function dvh(value: number): string {
if (supportsDVH) {
return `${value}dvh`
}
return `${value}vh`
}
7 changes: 7 additions & 0 deletions src/utils/test-helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ global.ResizeObserver = jest.fn().mockImplementation(() => {
disconnect: jest.fn()
}
})

global.CSS = {
escape: jest.fn(),
supports: jest.fn().mockImplementation(() => {
return false
})
}