Skip to content

Commit

Permalink
add onAutoScrollChange
Browse files Browse the repository at this point in the history
  • Loading branch information
mebtte committed Jun 12, 2024
1 parent 16afb21 commit b3ac882
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
cd storybook-static
git init
git config --local user.name mebtte
git config --local user.email hi@mebtte.com
git config --local user.email i@mebtte.com
git remote add origin https://${{secrets.ACCESS_TOKEN}}@github.com/mebtte/react-lrc.git
git switch -c gh-pages
git add -A
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ Call this when current line changed. `Line` is `LrcLine` when using `Lrc` compon

#### `recoverAutoScrollInterval`

The interval of recovering auto scroll after user scroll. It is `millisecond`, default `5000`.
The interval of recovering auto scroll after user scrolling. It is `millisecond`, default `5000`.

#### `onAutoScrollChange`?: ({ autoScroll: boolean }) => void

There is a state which indicates whether or not it is auto-scroll, and which default value is `true`. When scrolling by user, it will be set to `false`. After `recoverAutoScrollInterval` milliseconds, it will be set to `true` automatically. `onAutoScrollChange` will be called when the state changed.

### Component `Lrc`

Expand Down
3 changes: 3 additions & 0 deletions examples/lrc/auto_scroll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const Line = styled.div<{ active: boolean }>`
color: ${active ? 'green' : 'black'};
`}
`;
const log = console.log.bind(console);

function LrcDemo({
lrc,
Expand Down Expand Up @@ -85,6 +86,8 @@ function LrcDemo({
style={lrcStyle}
recoverAutoScrollSingal={signal}
recoverAutoScrollInterval={recoverAutoScrollInterval}
onLineUpdate={log}
onAutoScrollChange={log}
/>
</div>
</Root>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "react-lrc",
"version": "3.1.3",
"version": "3.2.0",
"description": "The react component to display lyric from LRC.",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"author": {
"name": "mebtte",
"email": "hi@mebtte.com",
"email": "i@mebtte.com",
"url": "https://mebtte.com"
},
"license": "MIT",
Expand Down
2 changes: 2 additions & 0 deletions src/components/base_lrc/base_lrc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function BaseLrc<Line extends BaseLine>(
recoverAutoScrollInterval = DEFAULT_PROPS.recoverAutoScrollInterval,
recoverAutoScrollSingal = DEFAULT_PROPS.recoverAutoScrollSingal,
onLineUpdate,
onAutoScrollChange,

onWheel,
onKeyDown,
Expand All @@ -53,6 +54,7 @@ function BaseLrc<Line extends BaseLine>(
} = useAutoScroll({
recoverAutoScrollInterval,
recoverAutoScrollSingal,
onAutoScrollChange,
});

useScrollAction({
Expand Down
2 changes: 1 addition & 1 deletion src/components/base_lrc/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseLine, BaseProps } from '../../constants';
import { type BaseLine, type BaseProps } from '../../constants';

export const LINE_CLASSNAME = 'react-lrc-line';

Expand Down
4 changes: 2 additions & 2 deletions src/components/base_lrc/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BaseLrc from './base_lrc';
import { Props } from './constants';
import { type Props } from './constants';

export { Props };
export type { Props };
export default BaseLrc;
14 changes: 12 additions & 2 deletions src/components/base_lrc/use_auto_scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
type KeyboardEvent,
} from 'react';
import throttle from '../../utils/throttle';
import { type Props } from './constants';
import { type BaseLine } from '../../constants';

const SCROLLABLE_KEYS = [' ', 'ArrowUp', 'ArrowDown'];

Expand All @@ -15,14 +17,16 @@ const SCROLLABLE_KEYS = [' ', 'ArrowUp', 'ArrowDown'];
* 1. wheel
* 2. keyboard
* 3. drag scrollbar
* @author mebtte<hi@mebtte.com>
* @author mebtte<i@mebtte.com>
*/
export default ({
recoverAutoScrollInterval,
recoverAutoScrollSingal,
onAutoScrollChange,
}: {
recoverAutoScrollInterval: number;
recoverAutoScrollSingal: boolean;
onAutoScrollChange: Props<BaseLine>['onAutoScrollChange'];
}) => {
const [autoScroll, setAutoScroll] = useState(true);

Expand Down Expand Up @@ -68,9 +72,15 @@ export default ({
setAutoScroll(true);
}, [recoverAutoScrollSingal]);

useEffect(() => {
if (onAutoScrollChange) {
onAutoScrollChange({ autoScroll });
}
}, [autoScroll, onAutoScrollChange]);

/**
* clear timer after unmount
* @author mebtte<hi@mebtte.com>
* @author mebtte<i@mebtte.com>
*/
useEffect(() => () => globalThis.clearTimeout(timerRef.current), []);

Expand Down
11 changes: 4 additions & 7 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import { ReactNode } from 'react';
import { type ReactNode } from 'react';

export interface BaseLine {
id: string;
startMillisecond: number;
}

export interface BaseProps<Line extends BaseLine> {
lineRenderer: ({
index,
active,
line,
}: {
lineRenderer: (payload: {
index: number;
active: boolean;
line: Line;
}) => ReactNode;
currentMillisecond?: number;
verticalSpace?: boolean;
onLineUpdate?: (line: { index: number; line: Line | null }) => void;
recoverAutoScrollInterval?: number;
recoverAutoScrollSingal?: boolean;
onLineUpdate?: (line: { index: number; line: Line | null }) => void;
onAutoScrollChange?: (payload: { autoScroll: boolean }) => void;
}

export const DEFAULT_PROPS = {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/get_random_string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* get random string
* @author mebtte<hi@mebtte.com>
* @author mebtte<i@mebtte.com>
*/
function getRandomString() {
return Math.random().toString(36).substring(2, 8);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/use_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRef, useCallback } from 'react';

/**
* stable useCallback
* @author mebtte<hi@mebtte.com>
* @author mebtte<i@mebtte.com>
*/
function useEvent<Callback extends (...args: unknown[]) => unknown>(
callback: Callback,
Expand Down

0 comments on commit b3ac882

Please sign in to comment.