Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added keyboard controls for entry vote slider #1569

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ exports[`(1) Default render 1`] = `
</div>
<div
className="editor-tool"
id="editor-tool-emoji-picker-074f17f0-0b9e-4c5f-ad68-6e462e23fb8b"
id="editor-tool-emoji-picker-eb89ac22-2702-4311-9bb6-70a8f5e909f8"
role="none"
title="Emoji"
>
Expand Down Expand Up @@ -584,7 +584,7 @@ exports[`(2) Cancellable, in progress 1`] = `
</div>
<div
className="editor-tool"
id="editor-tool-emoji-picker-15c51e4e-8605-497a-87f3-ae6f7bca17a6"
id="editor-tool-emoji-picker-01be801c-eb95-4ea8-b8cd-9a74f7a3ffac"
role="none"
title="Emoji"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,44 @@ Array [
<div
className="slider slider-up"
>
<div
className="css-yvszuv-Slider"
onMouseDown={[Function]}
onTouchStart={[Function]}
<span
className="w-full flex [&>div]:w-full"
onClick={[Function]}
onKeyUp={[Function]}
tabIndex={0}
>
<div
className="css-3g5hux-Slider"
style={
Object {
"width": "100%",
}
}
/>
<div
onClick={[Function]}
className="css-yvszuv-Slider"
onMouseDown={[Function]}
onTouchStart={[Function]}
style={
Object {
"left": "100%",
"position": "absolute",
"top": "50%",
"transform": "translate(-50%, -50%)",
}
}
>
<div
className="css-1ldz52l-Slider"
className="css-3g5hux-Slider"
style={
Object {
"width": "100%",
}
}
/>
<div
onClick={[Function]}
onMouseDown={[Function]}
onTouchStart={[Function]}
style={
Object {
"left": "100%",
"position": "absolute",
"top": "50%",
"transform": "translate(-50%, -50%)",
}
}
>
<div
className="css-1ldz52l-Slider"
/>
</div>
</div>
</div>
</span>
</div>
<div
className="percentage"
Expand Down
18 changes: 7 additions & 11 deletions src/common/components/entry-vote-btn/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { _t } from "../../i18n";
import "./_index.scss";
import { useMappedStore } from "../../store/use-mapped-store";
import { Button } from "@ui/button";
import Slider from "react-input-slider";
import { ReactInputSliderWrapper } from "../../features/packages-helpers";

const setVoteValue = (
type: "up" | "down" | "downPrevious" | "upPrevious",
Expand Down Expand Up @@ -324,11 +324,9 @@ export class VoteDialog extends Component<VoteDialogProps, VoteDialogState> {
</div>
<div className="space" />
<div className="slider slider-up">
<Slider
axis="x"
xstep={0.1}
x={upSliderVal}
onChange={({ x }) => this.upSliderChanged(x)}
<ReactInputSliderWrapper
value={upSliderVal}
onChange={(x) => this.upSliderChanged(x)}
/>
</div>
<div className="percentage">{`${upSliderVal && upSliderVal.toFixed(1)}%`}</div>
Expand Down Expand Up @@ -379,11 +377,9 @@ export class VoteDialog extends Component<VoteDialogProps, VoteDialogState> {
<FormattedCurrency {...this.props} value={this.estimate(downSliderVal)} fixAt={3} />
</div>
<div className="slider slider-down">
<Slider
axis="x"
xstep={0.1}
x={downSliderVal}
onChange={({ x }) => this.downSliderChanged(x)}
<ReactInputSliderWrapper
value={downSliderVal}
onChange={(x) => this.downSliderChanged(x)}
/>
</div>
<div className="space" />
Expand Down
1 change: 1 addition & 0 deletions src/common/features/packages-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./react-input-slider-wrapper";
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Slider from "react-input-slider";
import React, { useRef } from "react";

interface Props {
value: number;
onChange: (v: number) => void;
}

export function ReactInputSliderWrapper({ value, onChange }: Props) {
const rootRef = useRef<HTMLSpanElement | null>(null);

const handleKeyboard = (e: React.KeyboardEvent) => {
if (e.key === "ArrowLeft" && value > 0) {
onChange(value - 1);
}
if (e.key === "ArrowRight" && value < 100) {
onChange(value + 1);
}
};

return (
<span
ref={rootRef}
tabIndex={0}
className="w-full flex [&>div]:w-full"
onKeyUp={handleKeyboard}
onClick={() => rootRef.current?.focus()}
>
<Slider axis="x" xstep={0.1} x={value} onChange={({ x }) => onChange(x)} />
</span>
);
}