Skip to content

Commit

Permalink
refactor: use shared HorizontalScroll component for Chips and Calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
tymmesyde committed Jul 3, 2024
1 parent c6ad7e9 commit e7ddbcf
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 42 deletions.
15 changes: 0 additions & 15 deletions src/common/Chips/Chips.less
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
// Copyright (C) 2017-2024 Smart code 203358507

@mask-width: 10%;

.chips {
position: relative;
width: 100%;
display: flex;
align-items: center;
justify-content: flex-start;
gap: 1rem;
overflow-x: auto;

&.left {
mask-image: linear-gradient(90deg, rgba(0, 0, 0, 1) calc(100% - @mask-width), rgba(0, 0, 0, 0) 100%);
}

&.right {
mask-image: linear-gradient(90deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 1) @mask-width);
}

&.center {
mask-image: linear-gradient(90deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 1) @mask-width, rgba(0, 0, 0, 1) calc(100% - @mask-width), rgba(0, 0, 0, 0) 100%);
}
}
27 changes: 4 additions & 23 deletions src/common/Chips/Chips.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (C) 2017-2024 Smart code 203358507

import React, { memo, useEffect, useRef, useState } from 'react';
import classNames from 'classnames';
import React, { memo } from 'react';
import HorizontalScroll from '../HorizontalScroll';
import Chip from './Chip';
import styles from './Chips.less';

Expand All @@ -16,28 +16,9 @@ type Props = {
onSelect: (value: string) => {},
};

const SCROLL_THRESHOLD = 1;

const Chips = memo(({ options, selected, onSelect }: Props) => {
const ref = useRef<HTMLDivElement>(null);
const [scrollPosition, setScrollPosition] = useState('left');

useEffect(() => {
const onScroll = ({ target }: Event) => {
const { scrollLeft, scrollWidth, offsetWidth} = target as HTMLDivElement;
const position =
(scrollLeft - SCROLL_THRESHOLD) <= 0 ? 'left' :
(scrollLeft + offsetWidth + SCROLL_THRESHOLD) >= scrollWidth ? 'right' :
'center';
setScrollPosition(position);
};

ref.current?.addEventListener('scroll', onScroll);
return () => ref.current?.removeEventListener('scroll', onScroll);
}, []);

return (
<div ref={ref} className={classNames(styles['chips'], [styles[scrollPosition]])}>
<HorizontalScroll className={styles['chips']}>
{
options.map(({ label, value }) => (
<Chip
Expand All @@ -49,7 +30,7 @@ const Chips = memo(({ options, selected, onSelect }: Props) => {
/>
))
}
</div>
</HorizontalScroll>
);
});

Expand Down
20 changes: 20 additions & 0 deletions src/common/HorizontalScroll/HorizontalScroll.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2017-2024 Smart code 203358507

@mask-width: 10%;

.horizontal-scroll {
position: relative;
overflow-x: auto;

&.left {
mask-image: linear-gradient(90deg, rgba(0, 0, 0, 1) calc(100% - @mask-width), rgba(0, 0, 0, 0) 100%);
}

&.right {
mask-image: linear-gradient(90deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 1) @mask-width);
}

&.center {
mask-image: linear-gradient(90deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 1) @mask-width, rgba(0, 0, 0, 1) calc(100% - @mask-width), rgba(0, 0, 0, 0) 100%);
}
}
40 changes: 40 additions & 0 deletions src/common/HorizontalScroll/HorizontalScroll.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (C) 2017-2024 Smart code 203358507

import React, { useRef, useEffect, useState } from 'react';
import classNames from 'classnames';
import styles from './HorizontalScroll.less';

const SCROLL_THRESHOLD = 1;

type Props = {
className: string,
children: React.ReactNode,
};

const HorizontalScroll = ({ className, children }: Props) => {
const ref = useRef<HTMLDivElement>(null);
const [scrollPosition, setScrollPosition] = useState('left');

useEffect(() => {
const onScroll = ({ target }: Event) => {
const { scrollLeft, scrollWidth, offsetWidth } = target as HTMLDivElement;

setScrollPosition(() => (
(scrollLeft - SCROLL_THRESHOLD) <= 0 ? 'left' :
(scrollLeft + offsetWidth + SCROLL_THRESHOLD) >= scrollWidth ? 'right' :
'center'
));
};

ref.current?.addEventListener('scroll', onScroll);
return () => ref.current?.removeEventListener('scroll', onScroll);
}, []);

return (
<div ref={ref} className={classNames(styles['horizontal-scroll'], className, [styles[scrollPosition]])}>
{children}
</div>
);
};

export default HorizontalScroll;
4 changes: 4 additions & 0 deletions src/common/HorizontalScroll/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (C) 2017-2024 Smart code 203358507

import HorizontalScroll from './HorizontalScroll';
export default HorizontalScroll;
2 changes: 2 additions & 0 deletions src/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const MetaRow = require('./MetaRow');
const ModalDialog = require('./ModalDialog');
const Multiselect = require('./Multiselect');
const { HorizontalNavBar, VerticalNavBar } = require('./NavBar');
const { default: HorizontalScroll } = require('./HorizontalScroll');
const PaginationInput = require('./PaginationInput');
const PlayIconCircleCentered = require('./PlayIconCircleCentered');
const Popup = require('./Popup');
Expand Down Expand Up @@ -64,6 +65,7 @@ module.exports = {
ModalDialog,
Multiselect,
HorizontalNavBar,
HorizontalScroll,
VerticalNavBar,
PaginationInput,
PlayIconCircleCentered,
Expand Down
1 change: 0 additions & 1 deletion src/routes/Calendar/Table/Cell/Cell.less
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
gap: 1rem;
height: 0;
padding: 1rem;
overflow-x: scroll;

.item {
flex: none;
Expand Down
6 changes: 3 additions & 3 deletions src/routes/Calendar/Table/Cell/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React, { useMemo } from 'react';
import classNames from 'classnames';
import { Button, Image } from 'stremio/common';
import { Button, Image, HorizontalScroll } from 'stremio/common';
import styles from './Cell.less';

type Props = {
Expand Down Expand Up @@ -37,7 +37,7 @@ const Cell = ({ selected, monthInfo, date, items, onClick }: Props) => {
{date.day}
</div>
</div>
<div className={styles['body']}>
<HorizontalScroll className={styles['body']}>
{
items.map(({ id, name, poster, deepLinks }) => (
<Button key={id} className={styles['item']} href={deepLinks.metaDetailsStreams}>
Expand All @@ -49,7 +49,7 @@ const Cell = ({ selected, monthInfo, date, items, onClick }: Props) => {
</Button>
))
}
</div>
</HorizontalScroll>
</div>
);
};
Expand Down

0 comments on commit e7ddbcf

Please sign in to comment.