Skip to content

XYSlider

The XYSlider component allows users to select a value in a two-dimensional space, providing a more intuitive way to select values that require both horizontal and vertical input.

The slider automatically handles the selection of a value between a minimum and maximum range on the horizontal and vertical axes. The slider itself provides slots to customize its appearance and behavior. For example you can customize its background or the handle to make it more visually appealing or to fit the design of your UI.

Using the XYSlider component is straightforward. You can import it and use it in your application as follows:

import XYSlider from '@components/Basic/XYSlider/XYSlider';
const App = () => {
return (
<XYSlider maxX={150} maxY={150} value={{x: 50, y: 50}} />
);
};
export default App;
Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the component’s root element.
classstring""Additional CSS classes to apply to the component.
refXYSliderRef | undefinedundefinedA reference to the component that gives you access to its methods and the underlying HTML element. Useful if you need to set the value of the slider programmatically.
value{x:number, y: number}{ x: 50, y: 50 }The current value of the slider. Use this to control or set the slider’s position.
minXnumber0The minimum value that the slider can select on the X-axis.
minYnumber0The minimum value that the slider can select on the Y-axis.
maxXnumber100The maximum value that the slider can select on the X-axis.
maxYnumber100The maximum value that the slider can select on the Y-axis.
stepnumber1The base unit for value changes. Used primarily for gamepad inputs to calculate movement speed and precision.
onChange(value: {x:number, y: number}) => voidundefinedA function that is called every time the slider’s value has changed. It can be used to retrieve the up to date value of the slider.
onActionRecord<string, (scope?: string, ...args: any[]) => void>undefinedExtends or overrides the component’s default navigation action handlers. See Implemented Navigation Actions for details.
anchorstring | HTMLElementundefinedLinks navigation to another element. When the anchor element is focused, the stepper’s actions will execute. Can be a CSS selector or HTMLElement.

To interact with the XYSlider programmatically, you can use the XYSliderRef interface. This interface provides properties and methods to access and manipulate the component’s state.

PropertyTypeDescription
elementHTMLDivElementA reference to the XYSlider’s root HTML element, useful for DOM access or styling.
valueAccessor<{x: number, y: number}>The current value of the slider. Use value() to get the latest value.
MethodParametersReturn ValueDescription
changeValuenewValue: {x: number, y: number}voidProgrammatically sets a new value for the XYSlider.

Customizes the slider’s background. Use this slot to apply add a custom background to the slider, such as a gradient or an image.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the slider’s background.
classstring""Additional CSS classes to style the slider’s background.
childrenJSX.Element""Content to be rendered inside the background slot. This can be used to add custom elements or styles.
import XYSlider from '@components/Basic/XYSlider/XYSlider';
import Absolute from '@components/Layout/Absolute/Absolute';
const App = () => {
return (
<XYSlider>
<XYSlider.Background style={{ background: 'linear-gradient(to right, #ff7e5f, #feb47b)' }}>
<Absolute style={{ width: '100%', height: '100%', background: 'rgba(0, 0, 0, 0.2)' }}>
Custom Background
</Absolute>
</XYSlider.Background>
</XYSlider>
);
};
export default App;

Customizes the draggable handle of the slider. Use this slot to apply custom styles or classes to the slider’s handle.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the slider’s handle.
classstring""Additional CSS classes to style the slider’s handle.
import XYSlider from '@components/Basic/XYSlider/XYSlider';
const App = () => {
return (
<XYSlider>
<XYSlider.Handle style={{ width: '2vmax', height: '2vmax', background: 'black' }} />
</XYSlider>
);
};
export default App;

The XYSlider component implements the following navigation actions by default:

Action NameBehavior
panMoves the slider handle based on 2D axis input. Includes acceleration and smoothing logic.

You can extend the XYSlider with additional navigation actions or override the default behavior using the onAction prop:

import XYSlider from '@components/Basic/XYSlider/XYSlider';
<XYSlider
onAction={{
'select': () => console.log('Selecting XYSlider'),
'pan': (_, axis: [number, number]) => console.log(`x: ${axis[0]}, y: ${axis[1]}`),
}} />

For more information about navigation actions, see the Navigation component documentation.

  • To get the current value of the XYSlider component, use the onChange prop.
  • When you save the value in a state variable, you can use it in your UI to display or process the slider’s value.
  1. Define a state variable (e.g., using createSignal) to store the slider value.
  2. Pass a callback to the onChange prop that updates this state variable.
  3. Use the state variable wherever you need the slider’s value.
import XYSlider from '@components/Basic/XYSlider/XYSlider';
import { createSignal } from 'solid-js';
const App = () => {
const [sliderValue, setSliderValue] = createSignal({x: 50, y: 50});
return (
<>
<XYSlider onChange={(value) => setSliderValue(value)} />
<div>X: {sliderValue().x} | Y: {sliderValue().y}</div>
</>
);
};
export default App;

Set the value of the slider programmatically

Section titled “Set the value of the slider programmatically”

The Slider component lets you set the value through code.

  1. Declare a ref variable with type XYSliderRef and pass it to the XYSlider component.
  2. Call ref.changeValue() from anywhere (e.g., a keyboard event or external button) to set the value programmatically.
import XYSlider { XYSliderRef } from '@components/Basic/XYSlider/XYSlider';
const App = () => {
let sliderRef!: XYSliderRef;
const handleKeyPress = (e: KeyboardEvent) => {
if (e.keyCode !== 13) return
sliderRef.changeValue({x: 150, y: 50});
}
return (
<XYSlider ref={sliderRef} maxX={200} />
);
};
export default App;

Adjusting gamepad movement speed and precision

Section titled “Adjusting gamepad movement speed and precision”

The XYSlider is designed to work out-of-the-box with the pan action of the Navigation component which uses the right analogue stick of the gamepad.

To tune the feel of the gamepad control, adjust the step prop.

  • A smaller step (e.g., 1) allows for pixel-perfect precision but might be slow on large ranges.
  • A larger step (e.g., 10) makes the slider faster but less precise.
<XYSlider maxX={1000} step={5} /> // Increases base speed for large range