--- url: /reference/items/Draggable/examples.md --- # Examples ## Simple :::code-group ```js import { registerComponent } from '@studiometa/js-toolkit'; import { Draggable } from '@studiometa/ui'; registerComponent(Draggable); ``` ::: ## Fit bounds Use the `data-option-fit-bounds` attribute to keep the draggable target in the component's root element bounds on drop. :::code-group ```css html.dark { background-color: #222; color: #eee; } ``` ::: ## Strict fit bounds Use the `data-option-strict-fit-bounds` attribute to keep the draggable target in the component's root element bounds. :::code-group ```css html.dark { background-color: #222; color: #eee; } ``` ::: ## Margin Use the `data-option-margin` attribute to configure inner or outer margins. :::code-group ```css html.dark { background-color: #222; color: #eee; } ``` ::: ## Carousel like By disabling drag on the `y` axis with the `data-option-no-y` attribute and with the `data-option-fit-bounds` option enabled, we can begin implementing a draggable carousel. :::code-group ```css html.dark { background-color: #222; color: #eee; } ``` ::: ## Dynamic parent By default, the [`parent` getter](./js-api.md#parent) returns the component's root element. The getter can be overriden to get a dynamic parent that will be resolved when dropping the element defined by the [`target` getter](./js-api.md#target). In the following example, the [`Action` component](../Action/index.md) is used to toggle the `ring` class on each colored square when they are clicked. The `Draggable` component is extended to override the `parent` getter to find the first element in the DOM with the `ring` class and use it as a basis to calculate the bounds. :::code-group ```js import { registerComponent } from '@studiometa/js-toolkit'; import { Draggable as DraggableCore, Action } from '@studiometa/ui'; class Draggable extends DraggableCore { get parent() { return document.querySelector('.ring') ?? this.$el; } } registerComponent(Draggable); registerComponent(Action); ``` :::