Skip to content

Instantly share code, notes, and snippets.

@leehericks
Created September 22, 2021 01:10
Show Gist options
  • Save leehericks/cbba7a305dff07daa57ae487380656f9 to your computer and use it in GitHub Desktop.
Save leehericks/cbba7a305dff07daa57ae487380656f9 to your computer and use it in GitHub Desktop.
Turbo-aware Stimulus controller to drive navigation via select element
<html>
<body>
<div data-controller="select-navigation">
<select data-action="change->select-navigation#navigate">
<option selected="selected" value="path/to/0">0</option>
<option value="path/to/1">1</option>
<option value="path/to/2">2</option>
</select>
<turbo-frame id="my_frame" data-select-navigation-target="frame"></turbo-frame>
</div>
</body>
</html>
import { Controller } from "stimulus"
export default class extends Controller {
static get targets() {
return ["frame"]
}
navigate(event) {
let location = this.extract_location(event.target)
event.target.blur()
if (this.hasFrameTarget) {
this.frameTarget.src = location
}
else {
Turbo.visit(location)
}
}
extract_location(select) {
return select.options[select.selectedIndex].value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment