Skip to content

Commit cc441ed

Browse files
committed
Better layouts and more props
1 parent 78397d6 commit cc441ed

6 files changed

Lines changed: 65 additions & 49 deletions

File tree

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
REACT_APP_API_BASE_URL = "http://localhost:3678"
22
REACT_APP_WS_URL = "ws://localhost:3678/events"
3+
REACT_APP_KIOSK_MODE = "false"
4+
REACT_APP_HIDE_ON_DISCONNECT = "false"
5+
REACT_APP_LAYOUT = "auto"

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,22 @@ TODO: OS instructions (auto-login, open browser, etc)
6060

6161
First install the dependency:
6262
```
63-
npm install --save `@stronk-tech/spotify-player`
63+
npm install --save `@stronk-tech/react-librespot-controller`
6464
```
6565

6666
Then import the component and fill in your API endpoints:
6767
```
68-
import MediaPlayer from "@stronk-tech/spotify-player";
69-
<MediaPlayer websocketUrl={"ws://apollo:3678/events"} apiBaseUrl={"http://apollo:3678"} hideOnDisconnect={true} />
68+
import MediaPlayer from "@stronk-tech/react-librespot-controller";
69+
<MediaPlayer websocketUrl={"ws://apollo:3678/events"} apiBaseUrl={"http://apollo:3678"} hideOnDisconnect={false} />
7070
```
7171

72-
If you set `hideOnDisconnect` to true, the entire component will hide itself when there is no connection to the API endpoint. Otherwise it will display an error state (an retry the connection of course).
72+
### Props
73+
74+
- `hideOnDisconnect`: When `true`, the entire component will hide itself when there is no connection to the API endpoint. Otherwise it will display an error state.
75+
- `websocketUrl`: Full URL to the WebSocket endpoint of the `go-librespot` client.
76+
- `apiBaseUrl`: Full URL to the HTTP API endpoint of the `go-librespot` client.
77+
- `kioskMode`: When `true`, the component will fill the entire screen. Otherwise it will fill the available width and base the layout on the width only. It is recommended to leave this option `false` when importing the module into an existing webpage and to `true` when you are running the player standalone on a touch screen.
78+
- `layout`: Can be `auto`, `default`, `widescreen`, `portrait`. See the screenshots for how the layouts look like.
7379

7480
# Screen shots
7581
The player arranges itself based on the screen dimensions, with three possible layouts:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@stronk-tech/react-librespot-controller",
33
"description": "`go-librespot` squeezebox-alike web frontend for small touchscreens",
4-
"version": "0.1.4",
4+
"version": "0.1.5",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.esm.js",
77
"files": [

src/components/MediaPlayer.css

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
box-shadow: rgba(40, 44, 52, 0.5) 2px 4px 6px 2px;
1717
}
1818

19-
.spotify-player-spotify-card:hover {
20-
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.3);
21-
z-index: 3;
22-
position: relative;
19+
.kiosk {
20+
height: 100vh;
21+
width: 100vw;
2322
}
2423

2524
.spotify-player-info-container {
@@ -67,9 +66,7 @@
6766
}
6867

6968
.spotify-player-default-layout .spotify-player-left {
70-
justify-content: space-between;
71-
height: 100%;
72-
flex: 1;
69+
display: flex;
7370
}
7471

7572
.spotify-player-default-layout .spotify-player-right {

src/components/MediaPlayer.js

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ import VolumeControls from "./Controls/VolumeControls";
1313
import "./MediaPlayer.css";
1414

1515
const thresholdWidescreen = 800;
16-
const thresholdNarrowscreen = 450;
16+
const thresholdTallcreen = 500;
17+
const thresholdNarrowscreen = 500;
1718

18-
// TODO: add props for forcing a layout
19+
// TODO: add props for the WebSocket retries, etc.
1920
// TODO: add more comments, IE for props
2021
const MediaPlayer = ({
2122
websocketUrl = process.env.REACT_APP_WS_URL || "ws://localhost:3678/events",
2223
apiBaseUrl = process.env.REACT_APP_API_BASE_URL || "http://localhost:3678",
23-
hideOnDisconnect = false,
24+
kioskMode = (process.env.REACT_APP_KIOSK_MODE.toLowerCase() == "true") || false,
25+
hideOnDisconnect = (process.env.REACT_APP_HIDE_ON_DISCONNECT.toLowerCase() == "true") || false,
26+
layout = process.env.REACT_APP_LAYOUT || "auto",
2427
}) => {
2528
const {
2629
playerRef,
@@ -46,7 +49,6 @@ const MediaPlayer = ({
4649
const { width, height } = useComponentSize(playerRef); //< Retrieve dimensions of media player
4750
const [activeTab, setActiveTab] = useState("Info"); //< Text info view or Playlist selection
4851
const [selectedPlaylist, setSelectedPlaylist] = useState(null);
49-
const [arrowOffset, setArrowOffset] = useState(0);
5052

5153
useEffect(() => {
5254
if (!isConnected) {
@@ -66,49 +68,56 @@ const MediaPlayer = ({
6668
};
6769

6870
const handleTabSwitch = (newVal) => {
69-
if (newVal == "Info"){
71+
if (newVal == "Info") {
7072
setSelectedPlaylist(null);
7173
}
7274
setActiveTab(newVal);
7375
}
7476

75-
// Watch for dimension updates and inject appropriate CSS layout
76-
useEffect(() => {
77-
const updateLayout = () => {
78-
if (playerRef.current) {
79-
const layoutClass =
80-
width > thresholdWidescreen
81-
? "spotify-player-widescreen-layout"
82-
: width < thresholdNarrowscreen
83-
? "spotify-player-portrait-layout"
84-
: "spotify-player-default-layout";
85-
86-
// Remove previous layout classes
87-
playerRef.current.classList.remove(
88-
"spotify-player-widescreen-layout",
89-
"spotify-player-portrait-layout",
90-
"spotify-player-default-layout"
91-
);
92-
93-
// Add the current layout class
94-
playerRef.current.classList.add(layoutClass);
95-
}
96-
};
97-
updateLayout();
98-
}, [width, playerRef]);
99-
10077
// Hide the entire player if we are not connected to the API and the hideOnDisconnect prop is set.
10178
if (hideOnDisconnect && !isConnected) {
10279
return null;
10380
}
10481

105-
const albumTitle = ((selectedPlaylist && activeTab == "Playlists") ? selectedPlaylist.name : null) || null
106-
const albumSubtitle = (selectedPlaylist && activeTab == "Playlists") ? selectedPlaylist.owner.display_name : `Disc ${trackInfo?.disc_number || "N/A"}, Track ${trackInfo?.track_number || "N/A"}`
107-
const albumImage = ((selectedPlaylist && activeTab == "Playlists") ? selectedPlaylist.images[0]?.url : trackInfo?.album_cover_url) || null
82+
const albumTitle = ((selectedPlaylist && activeTab == "Playlists") ? selectedPlaylist.name : null) || null;
83+
const albumSubtitle = (selectedPlaylist && activeTab == "Playlists") ? selectedPlaylist.owner.display_name : `Disc ${trackInfo?.disc_number || "N/A"}, Track ${trackInfo?.track_number || "N/A"}`;
84+
const albumImage = ((selectedPlaylist && activeTab == "Playlists") ? selectedPlaylist.images[0]?.url : trackInfo?.album_cover_url) || null;
85+
let playerClass = "spotify-player-spotify-card";
86+
if (layout == "auto") {
87+
if (!playerRef.current) {
88+
layout = "default";
89+
} else if (kioskMode) {
90+
if (width > thresholdNarrowscreen && height > thresholdTallcreen) {
91+
layout = "default";
92+
} else if (width > height) {
93+
layout = "widescreen";
94+
} else {
95+
layout = "portrait";
96+
}
97+
} else {
98+
if (width > thresholdWidescreen) {
99+
layout = "widescreen";
100+
} else if (width < thresholdNarrowscreen) {
101+
layout = "portrait";
102+
} else {
103+
layout = "default";
104+
}
105+
}
106+
}
107+
if (layout == "default") {
108+
playerClass += " spotify-player-default-layout";
109+
} else if (layout == "widescreen") {
110+
playerClass += " spotify-player-widescreen-layout";
111+
} else if (layout == "portrait") {
112+
playerClass += " spotify-player-portrait-layout";
113+
}
114+
if (kioskMode) {
115+
playerClass += " kiosk";
116+
}
108117

109118
return (
110-
<div ref={playerRef} className="spotify-player-spotify-card">
111-
{width < thresholdNarrowscreen ? (
119+
<div ref={playerRef} className={playerClass}>
120+
{layout == "portrait" ? (
112121
<>
113122
<Header
114123
isConnected={isConnected}
@@ -167,7 +176,7 @@ const MediaPlayer = ({
167176
/>
168177
</div>
169178
</>
170-
) : width > thresholdWidescreen ? (
179+
) : layout == "widescreen" ? (
171180
<div className="spotify-player-info-container">
172181
<div className="spotify-player-left">
173182
<AlbumCard

src/standalone.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ body {
1919

2020
body #root {
2121
height: unset;
22-
padding: 2em;
22+
padding: 0;
23+
overflow-y: auto;
2324
}

0 commit comments

Comments
 (0)