Skip to content

Commit 75da379

Browse files
committed
Index page fully working with Geist UI
Includes: - Change bar type from Element to HTMLElement (style property doesn't exists on type Element). - Rewrite changeElementClass to changeBarColor with much more simpler functionality. - Change constants from tailwind class to Geist UI's theme color.
1 parent f81592d commit 75da379

File tree

1 file changed

+15
-47
lines changed

1 file changed

+15
-47
lines changed

pages/index.tsx

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,34 @@ import { Grid } from "@geist-ui/react"
99
/**
1010
* Tailwind class of the active bar
1111
*/
12-
const ACTIVE_BAR_CLASS = "bg-black"
12+
const ACTIVE_BAR_COLOR = "#111"
1313

1414
/**
1515
* Tailwind class of the default bar
1616
*/
17-
const INACTIVE_BAR_CLASS = "bg-red-600"
17+
const INACTIVE_BAR_COLOR = "#ff1a1a"
1818

1919
/**
2020
* Sorting speed in miliseconds
2121
*/
2222
const SORTING_SPEED = 50
2323

24-
interface changeElementClassParam {
25-
add?: string
26-
remove?: string
27-
}
28-
2924
/**
30-
* Change an element's classList, can be adding or removing item from classList depending on the
31-
* 2nd param's properties
32-
* @param element A bar DOM element which will be modified
33-
* @param operations An object containing 2 optional property, add and remove. The presence
34-
* of one or both of it will determine what kind of operation will be done to the classList
25+
* Change element backgroundColor to a given value
26+
* @param element The DOM element
27+
* @param toColor The value in which the background color will be changed to
3528
*/
36-
const changeElementClass = (
37-
element: Element,
38-
operations: changeElementClassParam
39-
): void => {
40-
let isAdd = operations.add
41-
let isRemove = operations.remove
42-
let isRemoveAndAdd = isRemove && isAdd
43-
44-
if (isRemoveAndAdd) {
45-
element.classList.remove(operations.remove)
46-
element.classList.add(operations.add)
47-
} else if (isAdd) {
48-
element.classList.add(operations.add)
49-
} else if (isRemove) {
50-
element.classList.remove(operations.remove)
51-
}
29+
const changeBarColor = (element: HTMLElement, toColor: string): void => {
30+
element.style.backgroundColor = toColor
5231
}
5332

5433
/**
5534
* Revert each bar in the given collection bars from active bar to default bar
5635
* @param bars HTMLCollection of bar elements
5736
*/
58-
const revertBarsColor = (bars: Element[]): void => {
37+
const revertBarsColor = (bars: HTMLElement[]): void => {
5938
for (const bar of bars) {
60-
changeElementClass(bar, {
61-
add: INACTIVE_BAR_CLASS,
62-
remove: ACTIVE_BAR_CLASS,
63-
})
39+
changeBarColor(bar, INACTIVE_BAR_COLOR)
6440
}
6541
}
6642

@@ -71,7 +47,7 @@ const revertBarsColor = (bars: Element[]): void => {
7147
*/
7248
const revertPreviousBarsColors = (
7349
prevBarsIndexes: [number, number],
74-
bars: HTMLCollectionOf<Element>
50+
bars: HTMLCollectionOf<HTMLElement>
7551
): void => {
7652
let [prevBar1Idx, prevBar2Idx] = prevBarsIndexes
7753

@@ -83,7 +59,7 @@ const revertPreviousBarsColors = (
8359
revertBarsColor(previousActiveBars)
8460
}
8561

86-
type BarToActiveBar = { element: Element; newHeight: number }
62+
type BarToActiveBar = { element: HTMLElement; newHeight: number }
8763

8864
/**
8965
* Turn each bar in the given array into an active bar, then set the height of it
@@ -93,12 +69,7 @@ type BarToActiveBar = { element: Element; newHeight: number }
9369
*/
9470
const makeBarsActive = (bars: BarToActiveBar[]): void => {
9571
for (const bar of bars) {
96-
changeElementClass(bar.element, {
97-
add: ACTIVE_BAR_CLASS,
98-
remove: INACTIVE_BAR_CLASS,
99-
})
100-
101-
// @ts-ignore
72+
changeBarColor(bar.element, ACTIVE_BAR_COLOR)
10273
bar.element.style.height = `${bar.newHeight}%`
10374
}
10475
}
@@ -107,22 +78,19 @@ const makeBarsActive = (bars: BarToActiveBar[]): void => {
10778
* Turn every bar in the given array into an active bar one by one, from first bar to last bar
10879
* @param bars Array/HTML Collection of the bar's DOM elements
10980
*/
110-
const postSortAnimation = (bars: HTMLCollectionOf<Element>): void => {
81+
const postSortAnimation = (bars: HTMLCollectionOf<HTMLElement>): void => {
11182
for (let n = 0; n < bars.length; n++) {
11283
setTimeout(() => {
11384
let nThBar = bars[n]
11485

115-
changeElementClass(nThBar, {
116-
remove: INACTIVE_BAR_CLASS,
117-
add: ACTIVE_BAR_CLASS,
118-
})
86+
changeBarColor(nThBar, ACTIVE_BAR_COLOR)
11987
}, n * 10)
12088
}
12189
}
12290

12391
const animateSelectionSort = (barHeights: number[], postSortFunc?: () => void): void => {
12492
const [arrayStates, animationSequence] = selectionSort(barHeights)
125-
const bars = document.getElementsByClassName("bar")
93+
const bars = document.getElementsByClassName("bar") as HTMLCollectionOf<HTMLElement>
12694

12795
for (let i = 0; i < animationSequence.length; i++) {
12896
let firstIteration = i === 0

0 commit comments

Comments
 (0)