Skip to content

Commit

Permalink
Fix scroll issue in search page (#86)
Browse files Browse the repository at this point in the history
Signed-off-by: Cintia Sanchez Garcia <cynthiasg@icloud.com>
  • Loading branch information
cynthia-sg committed Dec 13, 2022
1 parent 4fe2372 commit 9449e90
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 20 deletions.
44 changes: 26 additions & 18 deletions web/clo-ui/src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import classnames from 'classnames';
import { isString } from 'lodash';
import isNumber from 'lodash/isNumber';
import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { FaCaretLeft, FaCaretRight } from 'react-icons/fa';

import styles from './Pagination.module.css';
Expand Down Expand Up @@ -40,23 +40,31 @@ const getPaginationOptions = (currentPage: number, pageCount: number): (string |
return range;
};

const PaginationBtn: React.FC<ButtonProps> = (btnProps: ButtonProps) => (
<button
className={classnames('page-link rounded-0', {
'text-primary': !btnProps.disabled && btnProps.active !== btnProps.pageNumber,
})}
onClick={() => {
if (btnProps.active !== btnProps.pageNumber) {
btnProps.onChange(btnProps.pageNumber);
}
}}
aria-label={`Open ${isString(btnProps.content) ? btnProps.content : `page ${btnProps.pageNumber}`}`}
disabled={btnProps.disabled}
tabIndex={btnProps.disabled ? -1 : 0}
>
{btnProps.content || btnProps.pageNumber}
</button>
);
const PaginationBtn: React.FC<ButtonProps> = (btnProps: ButtonProps) => {
const buttonRef = useRef<HTMLButtonElement>(null);

return (
<button
ref={buttonRef}
className={classnames('page-link rounded-0', {
'text-primary': !btnProps.disabled && btnProps.active !== btnProps.pageNumber,
})}
onClick={() => {
if (btnProps.active !== btnProps.pageNumber) {
if (buttonRef && buttonRef.current) {
buttonRef.current.blur();
}
btnProps.onChange(btnProps.pageNumber);
}
}}
aria-label={`Open ${isString(btnProps.content) ? btnProps.content : `page ${btnProps.pageNumber}`}`}
disabled={btnProps.disabled}
// tabIndex={btnProps.disabled ? -1 : 0}
>
{btnProps.content || btnProps.pageNumber}
</button>
);
};

export const Pagination: React.FC<IPaginationProps> = (props: IPaginationProps) => {
const [totalPages, setTotalPages] = useState(Math.ceil(props.total / props.limit));
Expand Down
3 changes: 2 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"react-icons": "^4.7.1",
"react-router-dom": "^6.4.5",
"source-map-explorer": "^2.5.3",
"tinycolor2": "^1.4.2"
"tinycolor2": "^1.4.2",
"ua-parser-js": "^1.0.32"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.14.1",
Expand Down
20 changes: 20 additions & 0 deletions web/src/utils/browserDetect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const parser = require('ua-parser-js');

class BrowserDetect {
private ua: any = {};

public init() {
this.ua = parser(navigator.userAgent);
}

public isSafari(): boolean {
if (this.ua.browser.name.includes('Safari')) {
return true;
}
return false;
}
}

const browserDetect = new BrowserDetect();
browserDetect.init();
export default browserDetect;
9 changes: 8 additions & 1 deletion web/src/utils/scrollToTop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import browserDetect from './browserDetect';

const scrollToTop = (): void => {
window.scrollTo(0, 0);
const isSafari = browserDetect.isSafari();
window.scrollTo({
top: 0,
// @ts-ignore: Unreachable code error
behavior: isSafari ? 'instant' : 'auto',
});
};

export default scrollToTop;
5 changes: 5 additions & 0 deletions web/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8717,6 +8717,11 @@ typescript@^4.9.4:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.4.tgz#a2a3d2756c079abda241d75f149df9d561091e78"
integrity sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==

ua-parser-js@^1.0.32:
version "1.0.32"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-1.0.32.tgz#786bf17df97de159d5b1c9d5e8e9e89806f8a030"
integrity sha512-dXVsz3M4j+5tTiovFVyVqssXBu5HM47//YSOeZ9fQkdDKkfzv2v3PP1jmH6FUyPW+yCSn7aBVK1fGGKNhowdDA==

unbox-primitive@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/unbox-primitive/-/unbox-primitive-1.0.2.tgz#29032021057d5e6cdbd08c5129c226dff8ed6f9e"
Expand Down

0 comments on commit 9449e90

Please sign in to comment.