Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added position related APIs. #705

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions core/source/Dom.mint
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,34 @@ module Dom {
) : Promise(Void) {
`#{element}.scrollTo({ behavior: 'smooth', left: #{left}, top: #{top} })`
}

/*
Returns the distance from the outer border of the element (including its
margin) to the left padding edge of the closest positioned ancestor element.

If the element is not in the DOM or not an element is passed (for example
`TextNode`) then the value will be `0`.

if let Just(div) = Document.getElementBySelector("div") {
Dom.offsetLeft(div)
}
*/
fun offsetLeft (element : Dom.Element) : Number {
`#{element}.offsetLeft || 0`
}

/*
Returns the distance from the outer border of the element (including its
margin) to the top padding edge of the closest positioned ancestor element.

If the element is not in the DOM or not an element is passed (for example
`TextNode`) then the value will be `0`.

if let Just(div) = Document.getElementBySelector("div") {
Dom.offsetTop(div)
}
*/
fun offsetTop (element : Dom.Element) : Number {
`#{element}.offsetTop || 0`
}
}
4 changes: 4 additions & 0 deletions core/source/Html/Event.mint
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type Html.Event {
clientY : Number,
screenX : Number,
screenY : Number,
offsetX : Number,
offsetY : Number,
layerX : Number,
layerY : Number,
pageX : Number,
pageY : Number,

Expand Down
18 changes: 18 additions & 0 deletions core/tests/tests/Dom.mint
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,21 @@ suite "Dom.getChildren" {
(element : Dom.Element) { Array.size(Dom.getChildren(element)) == 3 })
}
}

suite "Dom.offsetLeft" {
test "returns the offsetLeft value of the element" {
<div/>
|> Test.Html.start()
|> Test.Context.assert(
(element : Dom.Element) { Dom.offsetLeft(element) == 8 })
}
}

suite "Dom.offsetTop" {
test "returns the offsetTop value of the element" {
<div/>
|> Test.Html.start()
|> Test.Context.assert(
(element : Dom.Element) { Dom.offsetTop(element) == 8 })
}
}
8 changes: 8 additions & 0 deletions runtime/src/normalize_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ export const normalizeEvent = (event) => {
return -1;
case "screenY":
return -1;
case "layerX":
return -1;
case "layerY":
return -1;
case "offsetX":
return -1;
case "offsetY":
return -1;
Comment on lines +105 to +112
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't these need to be changed (to 0) as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// onScroll
case "detail":
Expand Down
18 changes: 9 additions & 9 deletions src/assets/runtime.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/assets/runtime_test.js

Large diffs are not rendered by default.