Skip to content

Commit

Permalink
fix progress bar when switching layout
Browse files Browse the repository at this point in the history
  • Loading branch information
lhz516 committed Dec 28, 2023
1 parent 7712515 commit e9bacd0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ module.exports = {
'template-curly-spacing': [2, 'never'],
'@typescript-eslint/ban-ts-comment': 0,
'@typescript-eslint/no-misused-promises': 0,
'@typescript-eslint/no-unsafe-member-access': 0,
},
overrides: [
{
Expand Down
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
10 changes: 9 additions & 1 deletion src/ProgressBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class ProgressBar extends Component<ProgressBarProps, ProgressBarState> {
}, 200)
}

componentDidUpdate(): void {
initialize(): void {
const { audio } = this.props
if (audio && !this.hasAddedAudioEventListener) {
this.audio = audio
Expand All @@ -201,6 +201,14 @@ class ProgressBar extends Component<ProgressBarProps, ProgressBarState> {
}
}

componentDidMount(): void {
this.initialize()
}

componentDidUpdate(): void {
this.initialize()
}

componentWillUnmount(): void {
if (this.audio && this.hasAddedAudioEventListener) {
this.audio.removeEventListener('timeupdate', this.handleAudioTimeUpdate)
Expand Down
37 changes: 37 additions & 0 deletions stories/change-layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { useState } from 'react'
import AudioPlayer, { RHAP_UI } from '../src/index.tsx'
import { MAIN_LAYOUT } from '../src/constants.ts'

const expandedPlayerProps: {
layout: MAIN_LAYOUT
customControlsSection: RHAP_UI[]
customProgressBarSection: RHAP_UI[]
} = {
layout: 'stacked',
customControlsSection: [RHAP_UI.CURRENT_TIME, RHAP_UI.CURRENT_LEFT_TIME],
customProgressBarSection: [RHAP_UI.PROGRESS_BAR, RHAP_UI.MAIN_CONTROLS],
}

const ChangeLayout: React.FC = () => {
const [config, setConfig] = useState({})

const setStacked = () => setConfig(expandedPlayerProps)
const setHorizontal = () => setConfig({})

return (
<div className="container">
<button onClick={setStacked}>stacked</button>
<button onClick={setHorizontal}>horizontal</button>

<h1>Hello, audio player!</h1>
<AudioPlayer
src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-9.mp3"
volume={0.5}
layout="stacked-reverse"
{...config}
/>
</div>
)
}

export default ChangeLayout
11 changes: 11 additions & 0 deletions stories/layouts-advanced.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Meta, Story, Preview } from '@storybook/addon-docs/blocks';
import { SAMPLE_MP3_URL } from './utils'
import VolumePercentage from './volume-percentage.tsx'
import AudioPlayer, { RHAP_UI } from '../src/index.tsx'
import ChangeLayout from './change-layout.tsx'

<Meta title="Layouts - Advanced" component={AudioPlayer} />

Expand Down Expand Up @@ -257,3 +258,13 @@ class VolumePercentage extends PureComponent<{}, { volumeText: string }> {
}
/>
```

#### Change Layout

<Story name="Change Layout">
<ChangeLayout />
</Story>

```jsx
<ChangeLayout />
```
1 change: 1 addition & 0 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"include": [
"./src",
"./.eslintrc.js",
"./stories/**/*.tsx",
"babel.config.js",
"webpack.config.js"
]
Expand Down

0 comments on commit e9bacd0

Please sign in to comment.