Skip to content

Commit

Permalink
feat: support follow system auto set theme
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Jul 2, 2020
1 parent 6b624e9 commit aeb1630
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .releaserc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
{
// ci 发布 npm 后 更新 package.json 的 版本
"assets": ["package.json"],
"message": "chore(release): 🤖 ${nextRelease.version} [ci skip] ${nextRelease.notes}"
"message": "chore(release): 🤖 ${nextRelease.version} [ci skip]"
}
],
"@semantic-release/npm",
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ import "react-jinke-music-player/lib/styles/index.less";
@default-color: #d9d9d9;
@bg-color: #f7f8fa;
@border-color: #d9d9d9;
@light-theme-panel-bg: #fff;
@light-theme-controller-bg: #fff;
@panel-bg-light: #fff;
@controller-bg-light: #fff;
@music-player-panel-height: 80px;
@lists-panel-width: 480px;
@lists-panel-height: 410px;
Expand Down
4 changes: 2 additions & 2 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ const options = {
//if you want dynamic change current play audio you can change it [type `number` default `0`]
// playIndex: 0,

//color of the music player theme [ type: 'light' | 'dark' default `dark` ]
theme: 'dark',
//color of the music player theme [ type: 'light' | 'dark' | 'auto' default `dark` ]
theme: 'auto',

// Specifies movement boundaries. Accepted values:
// - `parent` restricts movement within the node's offsetParent
Expand Down
5 changes: 5 additions & 0 deletions example/example.less
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ body {
color: #1a1a1a;
font-size: 14px;
padding: 20px;

@media screen and (prefers-color-scheme: dark) {
background: #141414;
color: #fff;
}
}

button,
Expand Down
5 changes: 5 additions & 0 deletions src/config/mediaQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const MEDIA_QUERY = {
MOBILE: '(max-width: 768px) and (orientation : portrait)',
DARK_THEME: 'screen and (prefers-color-scheme: dark)',
LIGHT_THEME: 'screen and (prefers-color-scheme: light)',
}
6 changes: 4 additions & 2 deletions src/config/propTypes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import PropTypes from 'prop-types'
import Locale from './locale'
import { THEME } from './theme'
import { MODE } from './mode'

const playModePropTypes = PropTypes.oneOf([
'order',
Expand All @@ -14,8 +16,8 @@ export default {
PropTypes.object,
PropTypes.oneOf(Object.values(Locale)),
]),
theme: PropTypes.oneOf(['dark', 'light']),
mode: PropTypes.oneOf(['mini', 'full']),
theme: PropTypes.oneOf(Object.values(THEME)),
mode: PropTypes.oneOf(Object.values(MODE)),
defaultPlayMode: playModePropTypes,
playMode: playModePropTypes,
drag: PropTypes.bool,
Expand Down
1 change: 1 addition & 0 deletions src/config/theme.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const THEME = {
LIGHT: 'light',
DARK: 'dark',
AUTO: 'auto',
}
74 changes: 52 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import LOCALE_CONFIG from './locale'

import 'rc-slider/assets/index.css'
import 'rc-switch/assets/index.css'
import { MEDIA_QUERY } from './config/mediaQuery'

const IS_MOBILE = isMobile()

Expand Down Expand Up @@ -583,8 +584,8 @@ export default class ReactJkMusicPlayer extends PureComponent {
<span
className="sounds-icon"
{...(isMobile
? { onTouchStart: this.onSound }
: { onClick: this.onSound })}
? { onTouchStart: this.onResetVolume }
: { onClick: this.onResetVolume })}
>
<MdVolumeMuteIcon />
</span>
Expand Down Expand Up @@ -974,8 +975,9 @@ export default class ReactJkMusicPlayer extends PureComponent {
this.setState({ moveX: x, moveY: y })
}

onSound = () => {
this.setAudioVolume(this.state.currentAudioVolume)
onResetVolume = () => {
const { currentAudioVolume } = this.state
this.setAudioVolume(currentAudioVolume)
}
setAudioVolume = (value) => {
this.audio.volume = value
Expand Down Expand Up @@ -1607,31 +1609,58 @@ export default class ReactJkMusicPlayer extends PureComponent {
}
}

listenerIsMobile = ({ matches }) => {
this.setState({
isMobile: !!matches,
})
}
addMobileListener = () => {
this.media = window.matchMedia(
'(max-width: 768px) and (orientation : portrait)',
)
if (typeof this.media.addEventListener !== 'undefined') {
this.media.addEventListener('change', this.listenerIsMobile)
addMatchMediaListener = (query, handler) => {
const media = window.matchMedia(query)
if ('addEventListener' in media) {
media.addEventListener('change', handler)
} else {
this.media.addListener(this.listenerIsMobile)
media.addListener(handler)
}
return media
}
removeMobileListener = () => {
if (this.media) {
if (typeof this.media.addEventListener !== 'undefined') {
this.media.removeEventListener('change', this.listenerIsMobile)
removeMatchMediaListener = (media, handler) => {
if (media) {
if ('removeEventListener' in media) {
media.removeEventListener('change', handler)
} else {
this.media.removeListener(this.listenerIsMobile)
media.removeListener(handler)
}
this.media = undefined
}
}
addMobileListener = () => {
this.mobileMedia = this.addMatchMediaListener(
MEDIA_QUERY.MOBILE,
this.mobileMediaHandler,
)
}
removeMobileListener = () => {
this.removeMatchMediaListener(this.mobileMedia, this.mobileMediaHandler)
}
addSystemThemeListener = () => {
this.systemThemeMedia = this.addMatchMediaListener(
MEDIA_QUERY.DARK_THEME,
this.systemThemeMediaHandler,
)
}
removeSystemThemeListener = () => {
this.removeMatchMediaListener(
this.systemThemeMedia,
this.systemThemeMediaHandler,
)
}
mobileMediaHandler = ({ matches }) => {
this.setState({
isMobile: !!matches,
})
}

systemThemeMediaHandler = ({ matches }) => {
if (this.props.theme === THEME.AUTO) {
const theme = matches ? THEME.DARK : THEME.LIGHT
this.updateTheme(theme)
}
}

setDefaultAudioVolume = () => {
const { defaultVolume, remember } = this.props
//音量 [0-1]
Expand Down Expand Up @@ -1983,6 +2012,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
}
componentDidMount() {
this.addMobileListener()
this.addSystemThemeListener()
this.initPlayer()
this.onGetAudioInstance()
}
Expand Down
19 changes: 12 additions & 7 deletions src/styles/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
left: 0;
color: #fff;
height: @panel-height;
background-color: @panel-bg;
background-color: @panel-bg-dark;
z-index: 99;
transition: background-color @common-animate-time;

Expand Down Expand Up @@ -398,9 +398,14 @@
background-color: @progress-bar-bg-color-light !important;
}

.music-player-controller {
border-color: @primary-color-light;
background-color: @controller-bg-light;
}

.music-player-panel {
box-shadow: 0 1px 2px 0 rgba(0, 34, 77, 0.05);
background-color: @light-theme-panel-bg;
background-color: @panel-bg-light;
color: #7d7d7d;

.img-content {
Expand Down Expand Up @@ -479,7 +484,7 @@

.audio-lists-panel-header {
color: @font-color;
background-color: #fff;
background-color: @primary-color-light;
border-bottom: 1px solid #f4f4f7;

.line {
Expand Down Expand Up @@ -519,7 +524,7 @@
}

.react-jinke-music-player-mobile {
background-color: @light-theme-panel-bg;
background-color: @panel-bg-light;
color: @font-color;

&-cover {
Expand Down Expand Up @@ -562,7 +567,7 @@
}

.play-mode-title {
background-color: @light-theme-panel-bg;
background-color: @panel-bg-light;
color: @primary-color;
}
}
Expand Down Expand Up @@ -605,8 +610,8 @@
width: @player-width;
height: @player-height;
border-radius: 50%;
border: 2px solid @light-theme-controller-bg;
background-color: @light-theme-controller-bg;
border: 1px solid @controller-bg-dark;
background-color: @controller-bg-dark;
color: @primary-color;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);
font-size: 20px;
Expand Down
9 changes: 7 additions & 2 deletions src/styles/vars.less
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
@primary-color: #31c27c;
@primary-color-light: #fff;
@primary-color-dark: rgba(0, 0, 0, 0.75);

@default-color: #d9d9d9;
@bg-color: #f7f8fa;
@border-color: #d9d9d9;
@light-theme-panel-bg: #fff;
@light-theme-controller-bg: #fff;
@panel-bg-light: @primary-color-light;
@controller-bg-light: @primary-color-light;
@panel-bg-dark: @primary-color-dark;
@controller-bg-dark: @primary-color-dark;

@border-radius: 4px;
@music-player-panel-height: 80px;
Expand Down

0 comments on commit aeb1630

Please sign in to comment.