-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ChordDictionary): added basic module
- Loading branch information
Showing
18 changed files
with
773 additions
and
9 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
26 changes: 26 additions & 0 deletions
26
src/renderer/views/ChordDictionary/ChordDictionary.module.scss
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,26 @@ | ||
@import 'tokens'; | ||
|
||
.container { | ||
position: relative; | ||
display: flex; | ||
width: 100%; | ||
height: 100%; | ||
flex-direction: column; | ||
justify-content: flex-end; | ||
align-items: center; | ||
overflow: hidden; | ||
} | ||
|
||
.pitchbar { | ||
--Sidebar_minSize: 48px; | ||
--Button_textTransform: none; | ||
} | ||
|
||
.content { | ||
height: 100%; | ||
} | ||
|
||
.chordbar { | ||
--Sidebar_minSize: 200px; | ||
--Button_textTransform: none; | ||
} |
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,87 @@ | ||
import React, { useEffect, useMemo, useState } from 'react'; | ||
import { Outlet, useNavigate, useParams } from 'react-router-dom'; | ||
import classnames from 'classnames/bind'; | ||
import { Chord, Note } from 'tonal'; | ||
import { SidebarContainer } from '@la-jarre-a-son/ui'; | ||
|
||
import { useSettings } from 'renderer/contexts/Settings'; | ||
import { NOTE_NAMES, getKeySignature, getNoteInKeySignature } from 'renderer/helpers'; | ||
|
||
import ChordDictionaryChromaMenu from './ChordDictionaryChromaMenu'; | ||
import ChordDictionaryChordMenu from './ChordDictionaryChordMenu'; | ||
|
||
import styles from './ChordDictionary.module.scss'; | ||
|
||
const cx = classnames.bind(styles); | ||
|
||
const ChordDictionary: React.FC = () => { | ||
const { settings } = useSettings(); | ||
const { chordName } = useParams(); | ||
|
||
const navigate = useNavigate(); | ||
const { key, accidentals } = settings.notation; | ||
const keySignature = useMemo( | ||
() => getKeySignature(key, accidentals === 'sharp'), | ||
[key, accidentals] | ||
); | ||
|
||
const [chroma, setChroma] = useState<number | null>(null); | ||
const [chordType, setChordType] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
if (chroma !== null && chordType !== null) { | ||
const name = encodeURIComponent( | ||
`${getNoteInKeySignature(NOTE_NAMES[chroma], keySignature.notes)}${chordType}` | ||
); | ||
|
||
navigate(`./${name}`); | ||
} | ||
return () => {}; | ||
}, [navigate, chroma, chordType, keySignature]); | ||
|
||
useEffect(() => { | ||
const chord = chordName ? Chord.get(chordName) : null; | ||
|
||
if (chord && chord.tonic) { | ||
setChroma(Note.chroma(chord.tonic) ?? null); | ||
} | ||
|
||
if (chord && chord.aliases[0]) { | ||
setChordType(chord.aliases[0]); | ||
} | ||
}, [chordName]); | ||
|
||
return ( | ||
<SidebarContainer | ||
className={cx('container')} | ||
sidebar={ | ||
<ChordDictionaryChromaMenu | ||
keySignature={keySignature} | ||
selected={chroma} | ||
onSelect={setChroma} | ||
/> | ||
} | ||
sidebarProps={{ className: cx('pitchbar') }} | ||
contentProps={{ className: cx('content') }} | ||
size="xs" | ||
open | ||
inset | ||
> | ||
<SidebarContainer | ||
className={cx('container')} | ||
sidebar={<ChordDictionaryChordMenu selected={chordType} onSelect={setChordType} />} | ||
sidebarProps={{ className: cx('chordbar') }} | ||
contentProps={{ className: cx('content') }} | ||
size="sm" | ||
open | ||
inset | ||
> | ||
<Outlet /> | ||
</SidebarContainer> | ||
</SidebarContainer> | ||
); | ||
}; | ||
|
||
ChordDictionary.defaultProps = {}; | ||
|
||
export default ChordDictionary; |
61 changes: 61 additions & 0 deletions
61
src/renderer/views/ChordDictionary/ChordDictionaryChordMenu.tsx
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,61 @@ | ||
import React, { useEffect, useMemo, useRef } from 'react'; | ||
import classnames from 'classnames/bind'; | ||
import { Tab, TabList } from '@la-jarre-a-son/ui'; | ||
|
||
import { getChordTypes } from 'renderer/helpers'; | ||
|
||
import styles from './ChordDictionary.module.scss'; | ||
|
||
const cx = classnames.bind(styles); | ||
|
||
type Props = { | ||
selected: string | null; | ||
onSelect: (note: string) => void; | ||
}; | ||
|
||
const ChordDictionaryChordMenu: React.FC<Props> = ({ selected, onSelect }) => { | ||
const ref = useRef<HTMLElement>(); | ||
const chordTypes = useMemo(() => getChordTypes(), []); | ||
|
||
useEffect(() => { | ||
if (ref.current) { | ||
const currentEl: HTMLElement | null = ref.current.querySelector('[aria-selected=true]'); | ||
|
||
if (currentEl) { | ||
if (ref.current.scrollTop > currentEl.offsetTop) { | ||
currentEl.scrollIntoView({ behavior: 'smooth', block: 'start', inline: 'start' }); | ||
} | ||
if ( | ||
ref.current.scrollTop + ref.current.offsetHeight < | ||
currentEl.offsetTop + currentEl.offsetHeight | ||
) { | ||
currentEl.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'start' }); | ||
} | ||
} | ||
} | ||
}, [selected]); | ||
|
||
return ( | ||
<TabList | ||
ref={ref} | ||
className={cx('chordnav')} | ||
aria-label="Chord Types Navigation" | ||
direction="vertical" | ||
variant="ghost" | ||
block | ||
> | ||
{chordTypes.map((chordType) => ( | ||
<Tab | ||
key={chordType.aliases[0]} | ||
className={cx('tab')} | ||
selected={selected === chordType.aliases[0]} | ||
onClick={() => onSelect(chordType.aliases[0])} | ||
> | ||
<span className={cx('label')}>{chordType.aliases[0] || 'maj'}</span> | ||
</Tab> | ||
))} | ||
</TabList> | ||
); | ||
}; | ||
|
||
export default ChordDictionaryChordMenu; |
68 changes: 68 additions & 0 deletions
68
src/renderer/views/ChordDictionary/ChordDictionaryChromaMenu.tsx
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,68 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import classnames from 'classnames/bind'; | ||
import { Tab, TabList } from '@la-jarre-a-son/ui'; | ||
|
||
import { | ||
KeySignatureConfig, | ||
NOTE_NAMES, | ||
formatSharpsFlats, | ||
getNoteInKeySignature, | ||
} from 'renderer/helpers'; | ||
|
||
import styles from './ChordDictionary.module.scss'; | ||
|
||
const cx = classnames.bind(styles); | ||
|
||
type Props = { | ||
keySignature: KeySignatureConfig; | ||
selected: number | null; | ||
onSelect: (chroma: number) => void; | ||
}; | ||
|
||
const ChordDictionaryChromaMenu: React.FC<Props> = ({ keySignature, selected, onSelect }) => { | ||
const ref = useRef<HTMLElement>(); | ||
|
||
useEffect(() => { | ||
if (ref.current) { | ||
const currentEl: HTMLElement | null = ref.current.querySelector('[aria-selected=true]'); | ||
|
||
if (currentEl) { | ||
if (ref.current.scrollTop > currentEl.offsetTop) { | ||
currentEl.scrollIntoView({ behavior: 'smooth', block: 'start', inline: 'start' }); | ||
} | ||
if ( | ||
ref.current.scrollTop + ref.current.offsetHeight < | ||
currentEl.offsetTop + currentEl.offsetHeight | ||
) { | ||
currentEl.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'start' }); | ||
} | ||
} | ||
} | ||
}, [selected]); | ||
|
||
return ( | ||
<TabList | ||
ref={ref} | ||
className={cx('chromanav')} | ||
aria-label="Chroma Navigation" | ||
direction="vertical" | ||
variant="ghost" | ||
block | ||
> | ||
{NOTE_NAMES.map((note, index) => ( | ||
<Tab | ||
key={note} | ||
className={cx('tab')} | ||
onClick={() => onSelect(index)} | ||
selected={selected === index} | ||
> | ||
<span className={cx('label')}> | ||
{formatSharpsFlats(getNoteInKeySignature(note, keySignature.notes))} | ||
</span> | ||
</Tab> | ||
))} | ||
</TabList> | ||
); | ||
}; | ||
|
||
export default ChordDictionaryChromaMenu; |
Oops, something went wrong.