-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
picture-in-picture lyrics even when lyrics are not enabled
- Loading branch information
1 parent
be50720
commit 6ce8459
Showing
11 changed files
with
654 additions
and
489 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { ReactElement, useEffect, useRef } from "react"; | ||
|
||
import { useAuth, useLyricsContext, useSpotify } from "hooks"; | ||
import { IFormatLyricsResponse } from "utils"; | ||
|
||
interface ILyricLineProps { | ||
line: IFormatLyricsResponse["lines"][0]; | ||
type: "current" | "previous" | "next"; | ||
} | ||
|
||
export function LyricLine({ line, type }: ILyricLineProps): ReactElement { | ||
const { player } = useSpotify(); | ||
const { user } = useAuth(); | ||
const isPremium = user?.product === "premium"; | ||
const lineRef = useRef<HTMLButtonElement>(null); | ||
const { lyricsProgressMs, lyricTextColor, lyricLineColor, lyrics } = | ||
useLyricsContext(); | ||
|
||
const lineColors = { | ||
current: "#fff", | ||
previous: lyricLineColor + "80", | ||
next: lyricTextColor, | ||
}; | ||
|
||
useEffect(() => { | ||
const line = lineRef.current; | ||
if (!line) return; | ||
const currentLine = line.classList.contains("current"); | ||
if (currentLine) { | ||
line.scrollIntoView({ | ||
behavior: "smooth", | ||
block: "center", | ||
inline: "nearest", | ||
}); | ||
} | ||
}, [lyricsProgressMs]); | ||
|
||
return ( | ||
<button | ||
onClick={(e) => { | ||
e.stopPropagation(); | ||
if ( | ||
isPremium && | ||
line?.startTimeMs && | ||
player && | ||
lyrics?.syncType === "LINE_SYNCED" | ||
) { | ||
player.seek(Number(line.startTimeMs)); | ||
} | ||
}} | ||
className={`line ${type}`} | ||
dir="auto" | ||
ref={lineRef} | ||
> | ||
{line.words} | ||
<style jsx>{` | ||
.line { | ||
display: block; | ||
color: ${lyricTextColor}; | ||
background-color: transparent; | ||
border: none; | ||
width: 100%; | ||
text-align: left; | ||
padding-left: 144px; | ||
} | ||
.line.current { | ||
color: ${lineColors.current}; | ||
} | ||
.line.previous { | ||
color: ${lineColors.previous}; | ||
} | ||
.line.next { | ||
color: ${lineColors.next}; | ||
} | ||
.line:hover { | ||
color: ${lyrics?.colors ? lyricLineColor : "#fff"}; | ||
opacity: 1; | ||
} | ||
@media (max-width: 768px) { | ||
.line { | ||
padding-left: 0; | ||
} | ||
} | ||
@media (max-width: 658px) { | ||
.line { | ||
padding-left: 0; | ||
} | ||
} | ||
.line { | ||
font-size: 32px; | ||
font-weight: 700; | ||
letter-spacing: -0.04em; | ||
line-height: 54px; | ||
cursor: pointer; | ||
transition: all 0.1s ease-out 0s; | ||
} | ||
`}</style> | ||
</button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { | ||
createContext, | ||
Dispatch, | ||
PropsWithChildren, | ||
ReactElement, | ||
SetStateAction, | ||
useMemo, | ||
useState, | ||
} from "react"; | ||
|
||
import { useLyrics, useLyricsInPictureInPicture, useSpotify } from "hooks"; | ||
import { DisplayInFullScreen } from "types/spotify"; | ||
import { IFormatLyricsResponse } from "utils"; | ||
|
||
export interface ILyricsContext { | ||
lyricsProgressMs: number; | ||
lyricTextColor: string; | ||
lyricsBackgroundColor?: string; | ||
lyricLineColor: string; | ||
lyrics: IFormatLyricsResponse | null; | ||
lyricsError: string | null; | ||
lyricsLoading: boolean; | ||
setLyricsProgressMs: Dispatch<SetStateAction<number>>; | ||
setLyricLineColor: Dispatch<SetStateAction<string>>; | ||
setLyricTextColor: Dispatch<SetStateAction<string>>; | ||
setLyricsBackgroundColor: Dispatch<SetStateAction<string | undefined>>; | ||
} | ||
|
||
const LyricsContext = createContext<ILyricsContext | undefined>(undefined); | ||
|
||
export function LyricsContextContextProvider({ | ||
children, | ||
}: PropsWithChildren): ReactElement { | ||
const { displayInFullScreen, isPictureInPictureLyircsCanvas } = useSpotify(); | ||
const [lyricsProgressMs, setLyricsProgressMs] = useState(0); | ||
const [lyricLineColor, setLyricLineColor] = useState<string>("#fff"); | ||
const [lyricTextColor, setLyricTextColor] = useState<string>("#fff"); | ||
const [lyricsBackgroundColor, setLyricsBackgroundColor] = useState< | ||
string | undefined | ||
>(); | ||
const requestLyrics = !!( | ||
displayInFullScreen === DisplayInFullScreen.Lyrics || | ||
(isPictureInPictureLyircsCanvas && document.pictureInPictureElement) | ||
); | ||
|
||
const { lyrics, lyricsError, lyricsLoading } = useLyrics({ | ||
setLyricsBackgroundColor, | ||
requestLyrics, | ||
}); | ||
|
||
useLyricsInPictureInPicture({ | ||
setLyricsProgressMs, | ||
setLyricLineColor, | ||
setLyricTextColor, | ||
lyrics, | ||
lyricsBackgroundColor, | ||
setLyricsBackgroundColor, | ||
lyricTextColor, | ||
lyricLineColor, | ||
lyricsProgressMs, | ||
lyricsError, | ||
requestLyrics, | ||
}); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
lyricsProgressMs, | ||
lyricLineColor, | ||
lyricTextColor, | ||
lyricsBackgroundColor, | ||
lyrics, | ||
lyricsError, | ||
lyricsLoading, | ||
setLyricsProgressMs, | ||
setLyricLineColor, | ||
setLyricTextColor, | ||
setLyricsBackgroundColor, | ||
}), | ||
[ | ||
lyricsProgressMs, | ||
lyricLineColor, | ||
lyricTextColor, | ||
lyricsBackgroundColor, | ||
lyrics, | ||
lyricsError, | ||
lyricsLoading, | ||
] | ||
); | ||
|
||
return ( | ||
<LyricsContext.Provider value={value}>{children}</LyricsContext.Provider> | ||
); | ||
} | ||
|
||
export default LyricsContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useContext } from "react"; | ||
|
||
import LyricsContext, { ILyricsContext } from "context/LyricsContextProvider"; | ||
|
||
export function useLyricsContext(): ILyricsContext { | ||
const context = useContext(LyricsContext); | ||
if (!context) | ||
throw new Error( | ||
"useLyricsContext must be used within a LyricsContextProvider" | ||
); | ||
|
||
return context; | ||
} |
Oops, something went wrong.