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
6 changes: 2 additions & 4 deletions packages/@react-aria/datepicker/src/useDateSegment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import {DatePickerFieldState, DateSegment} from '@react-stately/datepicker';
import {DatePickerProps, DateValue} from '@react-types/datepicker';
import {DOMProps} from '@react-types/shared';
import {isIOS, isMac, mergeProps, useEvent, useId} from '@react-aria/utils';
import {getScrollParent, isIOS, isMac, mergeProps, scrollIntoView, useEvent, useId} from '@react-aria/utils';
import {labelIds} from './useDateField';
import {NumberParser} from '@internationalized/number';
import React, {HTMLAttributes, RefObject, useMemo, useRef} from 'react';
Expand Down Expand Up @@ -243,9 +243,7 @@ export function useDateSegment<T extends DateValue>(props: DatePickerProps<T> &

let onFocus = () => {
enteredKeys.current = '';
if (ref.current?.scrollIntoView) {
ref.current.scrollIntoView();
}
scrollIntoView(getScrollParent(ref.current) as HTMLElement, ref.current);

// Safari requires that a selection is set or it won't fire input events.
// Since usePress disables text selection, this won't happen by default.
Expand Down
56 changes: 1 addition & 55 deletions packages/@react-aria/selection/src/useSelectableCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import {FocusEvent, HTMLAttributes, Key, KeyboardEvent, RefObject, useEffect, useRef} from 'react';
import {focusSafely, getFocusableTreeWalker} from '@react-aria/focus';
import {FocusStrategy, KeyboardDelegate} from '@react-types/shared';
import {focusWithoutScrolling, mergeProps, useEvent} from '@react-aria/utils';
import {focusWithoutScrolling, mergeProps, scrollIntoView, useEvent} from '@react-aria/utils';
import {isCtrlKeyPressed, isNonContiguousSelectionModifier} from './utils';
import {MultipleSelectionManager} from '@react-stately/selection';
import {useLocale} from '@react-aria/i18n';
Expand Down Expand Up @@ -402,57 +402,3 @@ export function useSelectableCollection(options: SelectableCollectionOptions): S
}
};
}

/**
* Scrolls `scrollView` so that `element` is visible.
* Similar to `element.scrollIntoView({block: 'nearest'})` (not supported in Edge),
* but doesn't affect parents above `scrollView`.
*/
function scrollIntoView(scrollView: HTMLElement, element: HTMLElement) {
let offsetX = relativeOffset(scrollView, element, 'left');
let offsetY = relativeOffset(scrollView, element, 'top');
let width = element.offsetWidth;
let height = element.offsetHeight;
let x = scrollView.scrollLeft;
let y = scrollView.scrollTop;
let maxX = x + scrollView.offsetWidth;
let maxY = y + scrollView.offsetHeight;

if (offsetX <= x) {
x = offsetX;
} else if (offsetX + width > maxX) {
x += offsetX + width - maxX;
}
if (offsetY <= y) {
y = offsetY;
} else if (offsetY + height > maxY) {
y += offsetY + height - maxY;
}

scrollView.scrollLeft = x;
scrollView.scrollTop = y;
}

/**
* Computes the offset left or top from child to ancestor by accumulating
* offsetLeft or offsetTop through intervening offsetParents.
*/
function relativeOffset(ancestor: HTMLElement, child: HTMLElement, axis: 'left'|'top') {
const prop = axis === 'left' ? 'offsetLeft' : 'offsetTop';
let sum = 0;
while (child.offsetParent) {
sum += child[prop];
if (child.offsetParent === ancestor) {
// Stop once we have found the ancestor we are interested in.
break;
} else if (child.offsetParent.contains(ancestor)) {
// If the ancestor is not `position:relative`, then we stop at
// _its_ offset parent, and we subtract off _its_ offset, so that
// we end up with the proper offset from child to ancestor.
sum -= ancestor[prop];
break;
}
child = child.offsetParent as HTMLElement;
}
return sum;
}
1 change: 1 addition & 0 deletions packages/@react-aria/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export * from './useDescription';
export * from './platform';
export * from './useEvent';
export * from './useValueEffect';
export * from './scrollIntoView';
65 changes: 65 additions & 0 deletions packages/@react-aria/utils/src/scrollIntoView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/**
* Scrolls `scrollView` so that `element` is visible.
* Similar to `element.scrollIntoView({block: 'nearest'})` (not supported in Edge),
* but doesn't affect parents above `scrollView`.
*/
export function scrollIntoView(scrollView: HTMLElement, element: HTMLElement) {
let offsetX = relativeOffset(scrollView, element, 'left');
let offsetY = relativeOffset(scrollView, element, 'top');
let width = element.offsetWidth;
let height = element.offsetHeight;
let x = scrollView.scrollLeft;
let y = scrollView.scrollTop;
let maxX = x + scrollView.offsetWidth;
let maxY = y + scrollView.offsetHeight;

if (offsetX <= x) {
x = offsetX;
} else if (offsetX + width > maxX) {
x += offsetX + width - maxX;
}
if (offsetY <= y) {
y = offsetY;
} else if (offsetY + height > maxY) {
y += offsetY + height - maxY;
}

scrollView.scrollLeft = x;
scrollView.scrollTop = y;
}

/**
* Computes the offset left or top from child to ancestor by accumulating
* offsetLeft or offsetTop through intervening offsetParents.
*/
function relativeOffset(ancestor: HTMLElement, child: HTMLElement, axis: 'left'|'top') {
const prop = axis === 'left' ? 'offsetLeft' : 'offsetTop';
let sum = 0;
while (child.offsetParent) {
sum += child[prop];
if (child.offsetParent === ancestor) {
// Stop once we have found the ancestor we are interested in.
break;
} else if (child.offsetParent.contains(ancestor)) {
// If the ancestor is not `position:relative`, then we stop at
// _its_ offset parent, and we subtract off _its_ offset, so that
// we end up with the proper offset from child to ancestor.
sum -= ancestor[prop];
break;
}
child = child.offsetParent as HTMLElement;
}
return sum;
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ storiesOf('Date and Time/DateRangePicker/styling', module)
.add(
'errorMessage',
() => render({errorMessage: 'Dates must be after today', validationState: 'invalid'})
)
.add(
'in scrollable container',
() => (
<div style={{height: '200vh'}}>
{render({granularity: 'second'})}
</div>
)
);

function render(props = {}) {
Expand Down