1.0.2 • Published 4 years ago

@mebtte/react-lrc v1.0.2

Weekly downloads
7
License
MIT
Repository
github
Last release
4 years ago

react-lrc

The react component that display lyric from lrc format.

Screenshot

npm.io

Feature

  • Auto scroll smoothly
  • User srcollable
  • Custom style

Requirement

Usage

npm install --save @mebtte/react-lrc
import React, { useCallback } from 'react';
import { Lrc } from '@mebtte/react-lrc';

const Lyric = ({ lrc, currentTime }) => {
  const lineRenderer = useCallback(
    ({ lrcLine: { millisecond, content }, index, active }) => (
      <div style={{ textAlign: 'center', color: active ? 'green' : 'inherit' }}>
        {content}
      </div>
    ),
  );
  const onCurrentLineChange = useCallback(
    ({ lrcLine: { millisecond, content }, index }) =>
      console.log(index, millisecond, content),
    [],
  );

  return (
    <Lrc
      lrc={lrc}
      currentTime={currentTime}
      lineRenderer={lineRenderer}
      onCurrentLineChange={onCurrentLineChange}
    />
  );
};

export default Lyric;

Lrc Props

propdescriptiontypedefault
lrclrc stringstringrequired
lineRendererlrc line render method({ lrcLine: { id: string, millisecond: number, content: string }, index: number, active: boolean }) => ReactNoderequired
currentTimecurrent timenumber, millisecond0
autoScrollwhether auto scrollbooleantrue
autoScrollAfterUserScrollauto scroll after user scrollnumber, millisecond6000
spaceTopspace on lrc component top, percent of lrc componentnumber, 0~10.4
onCurrentLineChangewhen current line change({ index: number, lrcLine: { id: string, millisecond: number, content: string } | null }) => voidnull

Lrc Methods

methoddescriptiontype
scrollToCurrentLinescroll to current line and set auto scroll() => void
getCurrentLineget the current lrc line() => { lrcLine: { id: string, millisecond: number, content: string } | null, index: number }

Other API

parseLrc

import { parseLrc } from '@mebtte/react-lrc';

parseLrc(lrcString); // { id: string, millesecond: number: content: string }[]

useLrc

import React from 'react';
import { useLrc } from '@mebtte/react-lrc';

const Component = () => {
  const lrcLineList = useLrc(lrcString); // { id: string, millesecond: number: content: string }[]
  // ...
};

Example

Question

Why lrc component do not auto scroll ?

You probably do not give a height to Lrc. The height can make Lrc scrollable.

How to prevent user scroll ?

<Lrc
  style={{ overflow: 'hidden !important' }}
  autoScrollAfterUserScroll={0}
  {...otherProps}
/>

How to hide scrollbar ?

.lrc {
  /* webkit */
  ::-webkit-scrollbar {
    width: 0;
  }

  /* firefox */
  scrollbar-width: none;

  /* ie */
  -ms-overflow-style: none;
}
<Lrc className="lrc" {...otherProps} />

Typescript

@mebtte/react-lrc export type LrcLine.

import React from 'react';
import { LrcLine } from '@mebtte/react-lrc';

const Component = () => {
  const lineRenderer = React.useCallback(
    ({ lrcLine }: { lrcLine: LrcLine }) => {
      // ...
    },
    [],
  );
  // ...
  return <Lrc lineRenderer={lineRenderer} {...otherProps} />;
};

License

MIT

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago