Skip to content

Commit d55abb9

Browse files
author
Matt Shirley
committed
♻️ refactor the code for friendlier usage and v1 release
1 parent 19ffd09 commit d55abb9

25 files changed

+729
-501
lines changed

README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# react-midi-hook
2-
Hook to communicate with MIDI devices using MIDIAccess.
2+
> React hook to easily use MIDI inputs via the Web MIDI API.
33
44
## Install
55

@@ -10,20 +10,22 @@ yarn add --save react-midi-hook
1010
## Usage
1111

1212
```jsx
13-
import React, { Component } from 'react'
13+
import React from 'react'
1414
import useMidi from 'react-midi-hook'
1515

1616
export default function App() {}
17-
const { inputs, activeKeys, listenToInputs } = useMidi();
18-
19-
if (inputs) listenToInputs();
17+
const { pressedKeys } = useMidi();
2018

2119
return (
22-
<p>{activeKeys[0].letter}</p>
20+
<p>{pressedKeys[0].letter}</p>
2321
);
2422
}
2523
```
2624

25+
# Browser Support
26+
27+
The Web MIDI API is currently only supported on Edge, Chrome and Opera. Please refer to [Can I use](https://caniuse.com/#feat=midi) for up to date information.
28+
2729
## License
2830

2931
MIT © [matthewshirley](https://github.com/matthewshirley)

example/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"react": "link:../node_modules/react",
88
"react-dom": "link:../node_modules/react-dom",
99
"react-scripts": "link:../node_modules/react-scripts",
10-
"react-midi-hook": "link:.."
10+
"react-midi-hook": "link:..",
11+
"styled-components": "^5.1.0",
12+
"styled-system": "^5.1.5"
1113
},
1214
"scripts": {
1315
"start": "node ../node_modules/react-scripts/bin/react-scripts.js start",

example/src/App.js

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
import React from 'react'
22
import useMidi from 'react-midi-hook'
3+
4+
import KeyText from './components/KeyText';
5+
import StartButton from './components/StartButton';
6+
import InstructionText from './components/InstructionText';
7+
38
import './index.css';
49

510
const App = () => {
6-
const { inputs, activeKeys, listenToInputs } = useMidi();
11+
const { pressedKeys, inputs, isError, isReady, initialize } = useMidi();
712

8-
if (!inputs) {
9-
return <p>There are no detected devices.</p>
10-
} else {
11-
listenToInputs();
12-
}
13-
14-
const keysToRender = activeKeys.map((key, index) => {
15-
const keyNote = key.letter + key.octave;
16-
17-
if (index === activeKeys.length - 1) {
18-
return (<h1 key={keyNote}>{keyNote}</h1>)
19-
}
20-
21-
return (<h2 key={keyNote}>{keyNote}</h2>)
22-
});
23-
24-
const instructions = <p>Press a key on your keyboard</p>
25-
26-
return <div className="main">{keysToRender.length ? keysToRender : instructions}</div>
13+
return (
14+
<div>
15+
<StartButton initialize={initialize} />
16+
<div className="note">
17+
<KeyText pressedKeys={pressedKeys} />
18+
<InstructionText pressedKeys={pressedKeys} inputs={inputs} isReady={isReady} isError={isError} />
19+
</div>
20+
</div>
21+
);
2722
}
2823

2924
export default App
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from "react";
2+
import Text from "../Text";
3+
4+
const STRINGS = {
5+
errorOnAccess: 'Uh oh, the browser is not supported or there was an error',
6+
waitingForAccess: 'Waiting for access',
7+
waitingForConnection: 'Waiting for device',
8+
ready: 'Press a key on your MIDI device',
9+
}
10+
11+
export default function InstructionText({ pressedKeys, inputs, isReady, isError }) {
12+
if (pressedKeys.length > 0) return (null);
13+
14+
let stringToRender;
15+
16+
if (isError) {
17+
stringToRender = STRINGS['errorOnAccess']
18+
} else if (isReady && inputs.length > 0) {
19+
stringToRender = STRINGS['ready'];
20+
} else if (isReady) {
21+
stringToRender = STRINGS['waitingForConnection']
22+
} else if (!isReady) {
23+
stringToRender = STRINGS['waitingForAccess']
24+
}
25+
26+
return (
27+
<Text>{stringToRender}</Text>
28+
)
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './InstructionText';
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
import Text from '../Text';
3+
4+
export default function KeyText({ pressedKeys }) {
5+
if (pressedKeys.length === 0) return (null);
6+
7+
const keysToRender = pressedKeys.map((key, index) => {
8+
const keyNote = key.letter + key.octave;
9+
10+
if (index === pressedKeys.length - 1) {
11+
return (<Text as="h1" key={keyNote} px={2}>{keyNote}</Text>)
12+
}
13+
14+
return (<Text as="h2" key={keyNote} px={2}>{keyNote}</Text>)
15+
});
16+
17+
return keysToRender;
18+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './KeyText';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from "react";
2+
import styled from "styled-components";
3+
import { color, border, layout, space, typography } from "styled-system";
4+
5+
const StyledButton = styled.button`
6+
display: inline-block;
7+
cursor: pointer;
8+
text-transform: capitalize;
9+
10+
${color}
11+
${space}
12+
${border}
13+
${layout}
14+
${typography}
15+
`;
16+
17+
export default function StartButton({ initialize, ...props }) {
18+
return (
19+
<StyledButton onClick={initialize} {...props}>
20+
Connect Devices
21+
</StyledButton>
22+
);
23+
}
24+
25+
StartButton.defaultProps = {
26+
my: 3,
27+
width: "25%",
28+
fontSize: 2,
29+
fontWeight: "bold",
30+
bg: "green",
31+
color: "white",
32+
height: "48px",
33+
lineHeight: "48px",
34+
borderStyle: "solid",
35+
borderWidth: "1px",
36+
textAlign: "center",
37+
verticalAlign: "middle",
38+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "./StartButton";

example/src/components/Text/Text.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import styled from 'styled-components';
2+
import { space, typography } from 'styled-system';
3+
4+
export const Text = styled.p`
5+
${space}
6+
${typography}
7+
`;
8+
9+
Text.propTypes = {
10+
...typography.propTypes,
11+
};
12+
13+
Text.defaultProps = {
14+
fontSize: 2,
15+
};
16+
17+
export default Text;

example/src/components/Text/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './Text';

example/src/index.css

+1-13
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,7 @@ code {
1313
monospace;
1414
}
1515

16-
h1 {
17-
font-size: 12rem;
18-
padding-left: 1rem;
19-
padding-right: 1rem;
20-
}
21-
22-
h2 {
23-
font-size: 10rem;
24-
padding-left: 1rem;
25-
padding-right: 1rem;
26-
}
27-
28-
.main {
16+
.note {
2917
display: flex;
3018
flex-direction: row;
3119
justify-content: center;

0 commit comments

Comments
 (0)