An F# WebSharper binding for SortableJS, the drag-and-drop reorderable-list library — so you can build sortable lists and boards with strongly typed F#.
- SortableJS 1.15.x, imported as the npm package (
Import/ImportDefault), so the sample bundles it with esbuild and there is no CDN<script>to add. - The whole option surface is typed, including the AutoScroll, OnSpill, MultiDrag and Swap plugin options.
Sortableinstances, the statics (Sortable.Get,Closest,Create,Active,Dragged,Ghost,Clone,Version), theMountplugin loader and theSortable.utilsDOM helpers are all bound.
Try it live: Open the sample in your browser
The sample doubles as the binding's test case: five
lists sharing one group, the MultiDrag and Swap plugins, revert/remove on spill,
a scrollable AutoScroll list, the instance API (toArray, sort, save,
option, closest), the statics and every Sortable.utils helper — with all
listeners logged.
| Project | Description |
|---|---|
WebSharper.SortableJs |
The binding (netstandard2.0). |
WebSharper.SortableJs.Sample |
Static SPA sample, built with esbuild into dist/. |
dotnet add package WebSharper.SortableJsor, in Paket:
nuget WebSharper.SortableJs
The binding declares its npm dependency, so the package is pulled in automatically:
<NpmDependencies>
<NpmPackage Name="sortablejs" Version=">= 1.15.7 < 2.0.0" ResolutionStrategy="Max" />
</NpmDependencies>open WebSharper
open WebSharper.JavaScript
open WebSharper.SortableJs
[<JavaScript>]
let createBoard () =
// MultiDrag and Swap have to be mounted before the instances are created.
// AutoScroll and OnSpill are already mounted by the `sortablejs` entry point.
Sortable.Mount(MultiDrag(), Swap())
let el = As<HTMLElement> (JS.Document.GetElementById "todo")
let sortable =
Sortable.Create(
el,
Options(
Group = GroupOptions("board", Pull = true, Put = true),
Animation = 180,
GhostClass = "sortable-ghost",
DataIdAttr = "data-id",
Handle = ".grip",
MultiDrag = true,
SelectedClass = "multi-selected",
MultiDragKey = "CTRL",
OnEnd = (fun evt ->
// `oldIndex`/`newIndex` are `Optional<int>`.
let from = if Optional.isDefined evt.OldIndex then string evt.OldIndex.Value else "?"
let to' = if Optional.isDefined evt.NewIndex then string evt.NewIndex.Value else "?"
JS.Console.Log("moved from " + from + " to " + to')),
OnSelect = (fun evt -> JS.Console.Log("selected " + evt.Item.TextContent))
)
)
// The instance API.
sortable.ToArray() |> Array.iter (JS.Console.Log)
sortable.Sort([| "b"; "a" |], true)
sortable.Option("animation", box 400)
sortable.Destroy()Every list in a group shares items:
Options(Group = GroupOptions("board", Pull = [| "board" |], Put = true))The binding imports the package root (sortablejs), which is the ES module the
SortableJS docs use. That entry point mounts AutoScroll and both OnSpill
plugins, so these options work immediately:
- AutoScroll:
Scroll,ScrollSensitivity,ScrollSpeed,BubbleScroll,ForceAutoScrollFallback,ScrollFn; - OnSpill:
RevertOnSpill,RemoveOnSpill,OnSpill.
The two heavier plugins are opt-in. Mount them once, before creating any instance that uses them:
Sortable.Mount(MultiDrag(), Swap())Options(MultiDrag = true, SelectedClass = "multi-selected", MultiDragKey = "CTRL",
AvoidImplicitDeselect = false, OnSelect = …, OnDeselect = …)
Options(Swap = true, SwapClass = "swap-highlight")MultiDrag also adds Sortable.Utils.Select(el) and
Sortable.Utils.Deselect(el).
SortableEvent extends Dom.Event and carries Item, From, To, Clone,
Items, Clones, OldIndex/NewIndex and
OldDraggableIndex/NewDraggableIndex (all Optional<int>), PullMode,
OriginalEvent, SwapItem, and OldIndicies/NewIndicies (one
MultiDragIndex per multi-dragged item). OnMove receives a MoveEvent and the
original Dom.Event; returning null keeps SortableJS's default placement.
Sortable.Utils is a typed view of the library's helper object: On, Off,
Css (getter and setter overloads), Find, Is, Closest, ToggleClass,
Extend, Throttle, Clone, Index, NextTick, CancelNextTick,
DetectDirection, GetChild, Expando, Select and Deselect.
Sortable.Utils.Css(el, "outline", "2px solid #4f46e5")
let child = Sortable.Utils.GetChild(el, 1, sortable.Options, false)
let direction = Sortable.Utils.DetectDirection(el, sortable.Options) // Direction.Vertical / .HorizontalSortable.Active, Sortable.Dragged, Sortable.Ghost, Sortable.Clone,
Sortable.Get(el), instance Closest(el, …) and utils.Closest(el, …) can be
null/undefined; the binding keeps them as reference types, so check with
isNull (box x). Sortable.Option(name) returns the raw obj — use As<'T>
to recover its type.
- The binding imports the package root, so a bundle contains a single copy of
Sortable. Do not import
sortablejs/modular/*.esm.jsfrom application code as well: that would duplicate the module and breakSortable.activeandSortable.mount. - The sample is light themed by default and has a light/dark switcher that
persists the choice in
localStorage. Sortable.mountis variadic, soSortable.Mount(MultiDrag(), Swap())emitsSortable.mount(new MultiDrag(), new Swap())— the form the upstream docs use.
The sample is a static SPA. Build it, then serve dist/:
dotnet tool install -g dotnet-serve # once
dotnet tool restore && dotnet paket restore
dotnet build WebSharper.SortableJs.Sample/WebSharper.SortableJs.Sample.fsproj -c Release
dotnet serve -d dist -p 5223Open http://localhost:5223/. The page exercises:
Sortable.Create/new Sortable/Sortable.Geton five lists sharing oneGroupOptions(Pull,Put,RevertClone), withAnimation,Easing,GhostClass,ChosenClass,DragClass,DataIdAttr,Draggable,Handle,Filter,PreventOnFilter,Ignore,EmptyInsertThreshold,SwapThreshold,InvertSwap,Delay,DelayOnTouchOnly,TouchStartThreshold,FallbackOnBody,FallbackClass,ForceFallback,SupportPointer,SetDataandStore;- the
onStart,onChoose,onUnchoose,onSort,onUpdate,onAdd,onRemove,onChange,onEnd,onClone,onFilter,onMove,onSpill,onSelectandonDeselectlisteners (see the log at the bottom); Sortable.Mount(MultiDrag(), Swap()),MultiDragKey = "CTRL"multi-selection witholdIndicies/newIndicies, and Swap withSwapClass;RevertOnSpill(drop outside → item returns) andRemoveOnSpill(drop outside → item is removed);- AutoScroll with
Scroll,ScrollSensitivity,ScrollSpeed,BubbleScrollandForceAutoScrollFallback; - the instance API:
ToArray,Sort,Save,Option(getter and setter),Closest,Destroy, plus the staticsActive,Dragged,Ghost,Clone,VersionandGet; - every
Sortable.utilsmember:On/Off,Css,Find,Is,Closest,ToggleClass,Extend,Throttle,Clone,Index,NextTick,CancelNextTick,DetectDirection,GetChild,Expando,Select,Deselect; - persistence:
store.set, "Save order"/"Restore order" overlocalStorage.
Prerequisites: the .NET 11 SDK (the WebSharper 11 compiler runs on the .NET 11 runtime) and Node.js.
git clone https://github.com/dotnet-websharper/sortablejs.git
cd sortablejs
# dependencies are managed by Paket
dotnet tool restore
dotnet paket restore
# the binding
dotnet build WebSharper.SortableJs/WebSharper.SortableJs.fsproj -c Release
# the sample: WebSharper output goes to WebSharper.SortableJs.Sample/build,
# then esbuild bundles it into dist/
dotnet build WebSharper.SortableJs.Sample/WebSharper.SortableJs.Sample.fsproj -c Release
dotnet serve -d dist -p 5223dotnet fsi ./build.fsx -t WS-PackageThe resulting WebSharper.SortableJs.nupkg contains the binding assembly (with
its WebSharper metadata sidecar), the XML documentation and the package
dependencies.
.github/workflows/ghpages.yml builds the sample in Release and publishes
dist/ to the gh-pages branch with peaceiris/actions-gh-pages. The deploy
runs only from the default branch.
The binding's own code is Apache 2.0, but the JavaScript library it binds is
licensed differently — MIT (SPDX: MIT) — for sortablejs 1.15.7,
copyright © 2019 All contributors to Sortable.
- Upstream license: https://github.com/SortableJS/Sortable/blob/1.15.7/LICENSE
- Upstream project: https://github.com/SortableJS/Sortable
MIT is permissive, not copyleft: it allows commercial and closed-source use, and there is no obligation to publish your own code. The one requirement is that the copyright notice and permission notice travel with copies of the library — which matters when you ship the bundled JavaScript the binding produces (the notice is included in the npm package and preserved by the bundler's license comments). The binding itself ships no upstream source, only the npm dependency declaration.
Apache 2.0 — see LICENSE.md.
