-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
117 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,27 @@ | ||
import { | ||
Behavior, | ||
map, | ||
snapshot, | ||
stepper, | ||
Stream, | ||
time | ||
} from "@funkia/hareactive"; | ||
import { elements, fgo, modelView, runComponent } from "../../src/index"; | ||
import { map, snapshot, stepper, Stream, time } from "@funkia/hareactive"; | ||
import { elements as E, runComponent, component } from "../../src/index"; | ||
|
||
const { p, button, h1 } = elements; | ||
const formatTime = (t: number) => new Date(t).toTimeString().split(" ")[0]; | ||
|
||
const formatTime = (t: number): string => | ||
new Date(t).toTimeString().split(" ")[0]; | ||
|
||
type ToView = { | ||
time: Behavior<number>; | ||
message: Behavior<string>; | ||
}; | ||
|
||
type ViewOut = { | ||
type On = { | ||
snapClick: Stream<any>; | ||
}; | ||
|
||
const model = fgo(function*({ snapClick }: ViewOut) { | ||
const main = component<On>((on, start) => { | ||
const msgFromClick = map( | ||
(t) => "You last pressed the button at " + formatTime(t), | ||
snapshot(time, snapClick) | ||
snapshot(time, on.snapClick) | ||
); | ||
const message = yield stepper( | ||
"You've not clicked the button yet", | ||
msgFromClick | ||
const message = start( | ||
stepper("You've not clicked the button yet", msgFromClick) | ||
); | ||
return { time, message }; | ||
}); | ||
|
||
const view = ({ time, message }: ToView) => [ | ||
h1("Continuous time example"), | ||
p(map(formatTime, time)), | ||
p(button("Click to snap time").use({ snapClick: "click" })), | ||
p(message) | ||
]; | ||
|
||
const main = modelView<ToView, ViewOut>(model, view)(); | ||
return [ | ||
E.h1("Continuous time example"), | ||
E.p(map(formatTime, time)), | ||
E.p(E.button("Click to snap time").use({ snapClick: "click" })), | ||
E.p(message) | ||
]; | ||
}); | ||
|
||
runComponent("#mount", main); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
import { elements } from "../../../src"; | ||
const { div, button } = elements; | ||
import { elements as E } from "../../../src"; | ||
|
||
// Counter | ||
const counterView = div([ | ||
const counterView = E.div([ | ||
"Counter ", | ||
1, | ||
" ", | ||
button({ class: "btn btn-default" }, "+"), | ||
E.button({ class: "btn btn-default" }, "+"), | ||
" ", | ||
button({ class: "btn btn-default" }, " - ") | ||
E.button({ class: "btn btn-default" }, " - ") | ||
]); | ||
|
||
export const main1 = counterView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,52 @@ | ||
import { | ||
map, | ||
streamFromEvent, | ||
stepper, | ||
toggle, | ||
snapshot, | ||
switcher, | ||
Behavior, | ||
combine, | ||
lift, | ||
runNow, | ||
Stream, | ||
accum | ||
} from "@funkia/hareactive"; | ||
import { runComponent, elements, go, fgo, modelView } from "../../../src"; | ||
const { div } = elements; | ||
import * as H from "@funkia/hareactive"; | ||
import { streamFromEvent } from "@funkia/hareactive/dom"; | ||
import { runComponent, elements as E, component } from "../../../src"; | ||
|
||
type Point = { x: number; y: number }; | ||
|
||
const mousemove = streamFromEvent(window, "mousemove"); | ||
const mousePosition = runNow( | ||
stepper({ x: 0, y: 0 }, mousemove.map((e) => ({ x: e.pageX, y: e.pageY }))) | ||
); | ||
const mousemove = streamFromEvent(window, "mousemove", (e) => ({ | ||
x: e.pageX, | ||
y: e.pageY | ||
})); | ||
|
||
const addPoint = (p1: Point, p2: Point) => ({ | ||
x: p1.x + p2.x, | ||
y: p1.y + p2.y | ||
}); | ||
|
||
type BoxModelInput = { | ||
startDrag: Stream<void>; | ||
endDrag: Stream<void>; | ||
startDrag: H.Stream<void>; | ||
endDrag: H.Stream<void>; | ||
}; | ||
|
||
const boxModel = fgo(function*( | ||
{ startDrag, endDrag }: BoxModelInput, | ||
color: string | ||
) { | ||
const startDragAt = snapshot(mousePosition, startDrag); | ||
const dragOffset = map( | ||
(p) => map((p2) => ({ x: p2.x - p.x, y: p2.y - p.y }), mousePosition), | ||
startDragAt | ||
); | ||
const offset: Behavior<Point> = yield switcher( | ||
Behavior.of({ x: 0, y: 0 }), | ||
combine(dragOffset, endDrag.mapTo(Behavior.of({ x: 0, y: 0 }))) | ||
); | ||
const committed: Behavior<Point> = yield accum( | ||
addPoint, | ||
{ x: 0, y: 0 }, | ||
snapshot(offset, endDrag) | ||
); | ||
const position = lift(addPoint, committed, offset); | ||
const isBeingDragged = yield toggle(false, startDrag, endDrag); | ||
return { isBeingDragged, position }; | ||
}); | ||
|
||
type BoxViewInput = { | ||
position: Behavior<Point>; | ||
isBeingDragged: Behavior<boolean>; | ||
}; | ||
|
||
const boxView = ({ isBeingDragged, position }: BoxViewInput, color: string) => | ||
div({ | ||
class: ["box", { dragged: isBeingDragged }], | ||
style: { | ||
background: color, | ||
left: position.map(({ x }) => x + "px"), | ||
top: position.map(({ y }) => y + "px") | ||
} | ||
}).use({ startDrag: "mousedown", endDrag: "mouseup" }); | ||
|
||
const box = modelView(boxModel, boxView); | ||
|
||
const main = go(function*() { | ||
yield box("red"); | ||
}); | ||
|
||
runComponent("#mount", main); | ||
const box = (color: string) => | ||
component<BoxModelInput>((on, start) => { | ||
const mousePosition = start(H.stepper({ x: 0, y: 0 }, mousemove)); | ||
const startDragAt = H.snapshot(mousePosition, on.startDrag); | ||
const dragOffset = H.map( | ||
(p) => H.map((p2) => ({ x: p2.x - p.x, y: p2.y - p.y }), mousePosition), | ||
startDragAt | ||
); | ||
const offset: H.Behavior<Point> = start( | ||
H.switcher( | ||
H.Behavior.of({ x: 0, y: 0 }), | ||
H.combine(dragOffset, on.endDrag.mapTo(H.Behavior.of({ x: 0, y: 0 }))) | ||
) | ||
); | ||
const committed: H.Behavior<Point> = start( | ||
H.accum(addPoint, { x: 0, y: 0 }, H.snapshot(offset, on.endDrag)) | ||
); | ||
const position = H.lift(addPoint, committed, offset); | ||
const isBeingDragged = start(H.toggle(false, on.startDrag, on.endDrag)); | ||
|
||
return E.div({ | ||
class: ["box", { dragged: isBeingDragged }], | ||
style: { | ||
background: color, | ||
left: position.map(({ x }) => x + "px"), | ||
top: position.map(({ y }) => y + "px") | ||
} | ||
}).use({ startDrag: "mousedown", endDrag: "mouseup" }); | ||
}); | ||
|
||
runComponent("#mount", box("red")); |
Oops, something went wrong.