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

Add scroll_top() #174

Merged
merged 7 commits into from
Apr 5, 2018
Merged

Add scroll_top() #174

merged 7 commits into from
Apr 5, 2018

Conversation

rickycodes
Copy link
Contributor

Just floating this for some early feedback, still need to add docs, but thought I'd open a PR...

example usage (which I am doing for my own website: ricky.codes):

let top = Some(window().page_y_offset()).unwrap_or(document().document_element().unwrap().scroll_top());

and then setting:

document().document_element().unwrap().set_scroll_top(top.parse::<u32>().unwrap());

}

/// scrollTop
fn set_scroll_top( &self, value: u32 ) {
Copy link
Owner

Choose a reason for hiding this comment

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

Hmm... is there any particular reason why the getter returns i32 and the setter accepts u32?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@koute ah, good catch... I guess I should pick one 🤦‍♂️

what do you think makes the most sense here?

Copy link
Owner

Choose a reason for hiding this comment

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

In general we should stick with the types from the specs, so it'd be the best if you'd find the relevant IDL in the specs and use the type specified there. (:

Copy link
Contributor Author

@rickycodes rickycodes Apr 1, 2018

Choose a reason for hiding this comment

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

@koute the IDL states "unrestricted double"

image

so I guess u32?

Copy link
Contributor

Choose a reason for hiding this comment

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

Unrestricted double should be f64 no?
If you scroll down to TypedArray objects table, you can see what the Web IDL types represent.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

Copy link
Contributor Author

@rickycodes rickycodes Apr 2, 2018

Choose a reason for hiding this comment

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

Just want to do some double checking... Here's some example code of mine:

let top: f64 = Some(window().page_y_offset() as f64)
        .unwrap_or(document().document_element().unwrap().scroll_top());

I can see now why I left it as i32 to start...

page_y_offset is listed as:

image

does this make sense? or does page_y_offset need a different type as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

@rickycodes The double type should also be f64, therefore page_y_offset should return f64.

The only difference between double and unrestricted double is that in the WebIDL they guarantee that double will not be NaN, -Infinity, or Infinity (but the type is the same).

Copy link
Contributor Author

@rickycodes rickycodes Apr 2, 2018

Choose a reason for hiding this comment

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

Ok, cool. I just updated that as well. If things look good I can update with documentation/links and this should be good to go :)

Oh, I guess it would also make sense to add page_x_offset and scroll_left as well ;)

Copy link
Owner

Choose a reason for hiding this comment

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

Yep. Still needs the docs and the links to the specs. (:

Copy link
Contributor Author

@rickycodes rickycodes Apr 3, 2018

Choose a reason for hiding this comment

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

Ok, this should be all good now!

@@ -116,4 +116,17 @@ impl Document {
js!( @(no_return) @{self}.title = @{title}; );
}
}

/// Returns the Element that is the root element of the document (for example, the <html>
Copy link
Owner

Choose a reason for hiding this comment

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

The <html> has to be escaped in backticks or it will mess up the docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this should be good now, I've also gone back and added backticks that were missed in some of my previous other work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also updated doc links and the change to document_element() (:
lmk if you spot anything else!

/// element for HTML documents).
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement)
// https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-87CD092
Copy link
Owner

Choose a reason for hiding this comment

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

Please link to this spec instead: https://dom.spec.whatwg.org/#ref-for-dom-document-documentelement

Ditto for other links.

///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement)
// https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-87CD092
pub fn document_element( &self ) -> Option< HtmlElement > {
Copy link
Owner

Choose a reason for hiding this comment

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

According to specs shouldn't this return Option< Element >?

Copy link
Owner

@koute koute left a comment

Choose a reason for hiding this comment

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

LGTM, although the links to the specs still need to be fixed. (:

/// Gets the the number of pixels that an element's content is scrolled vertically.
///
/// [(Javascript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop)
// https://drafts.csswg.org/cssom-view/#dom-element-scrolltop
Copy link
Owner

Choose a reason for hiding this comment

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

For consistency please link to the IDL in the specs; for this particular method in should be:

https://drafts.csswg.org/cssom-view/#ref-for-dom-element-scrolltop%E2%91%A0

(:

/// aligned with the top edge of the window's content area.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/pageYOffset)
// https://drafts.csswg.org/cssom-view/#dom-window-pageyoffset
Copy link
Owner

Choose a reason for hiding this comment

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

These should also link to the IDL, e.g. in this case https://drafts.csswg.org/cssom-view/#ref-for-dom-window-pageyoffset

@rickycodes
Copy link
Contributor Author

@koute ack! 🤦‍♂️ ok, I hope this covers em'

thanks for catching these!

@@ -190,4 +190,27 @@ impl Window {
return @{self}.outerHeight;
).try_into().unwrap()
}

/// he read-only Window property pageYOffset is an alias for scrollY; as such, it returns
Copy link
Owner

Choose a reason for hiding this comment

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

he -> The (:

@koute
Copy link
Owner

koute commented Apr 5, 2018

Besides the typo LGTM. (:

@koute koute merged commit 003ef26 into koute:master Apr 5, 2018
@rickycodes rickycodes deleted the add-scroll-top branch October 7, 2019 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants