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

Introduce FrameElement.getElementById(id: string) #1136

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

seanpdoyle
Copy link
Contributor

The FrameElement.getElementById(id: string) static method unifies the internal logic to locate a <turbo-frame> element by its [id], while providing that consistent behavior to consumer applications.

Copy link

@sfnelson sfnelson left a comment

Choose a reason for hiding this comment

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

We have extensions to FrameElement for custom event handling that also need this type of detection, but as getFrameElementById is not visible we've had to re-implement that logic.

This looks like a useful enhancement 👍

@@ -162,9 +162,9 @@ export class Session {
const frameTarget = element.getAttribute("data-turbo-frame")
const frame = frameTarget == "_top" ?
null :
document.getElementById(frameTarget) || findClosestRecursively(element, "turbo-frame:not([disabled])")
FrameElement.getElementById(frameTarget) || findClosestRecursively(element, "turbo-frame")

Choose a reason for hiding this comment

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

This looks like it could change the logic – e.g. a nested frame situation where the nearest frame is disabled.

In the old code, the nearest frame would be skipped and the outer frame returned, in the new logic nearest frame would be returned and the subsequent conditional would fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
FrameElement.getElementById(frameTarget) || findClosestRecursively(element, "turbo-frame")
FrameElement.getElementById(frameTarget) || findClosestRecursively(element, "turbo-frame:not([disabled])")


if (isUnsafe || isStream || frame instanceof FrameElement) {
if (isUnsafe || isStream || (frame && !frame.disabled)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sfnelson since FrameElement.getElementById accounts for instanceof FrameElement, does this property check account for the concerns you've shared in https://github.com/hotwired/turbo/pull/1136/files/b750a8f2e44ef3133baf6328e5187c80ae5cf2e1#r1496721094?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the sake of shrinking the diff, both of these lines could likely be reverted in part:

Suggested change
if (isUnsafe || isStream || (frame && !frame.disabled)) {
if (isUnsafe || isStream || frame instanceof FrameElement) {

Copy link

@sfnelson sfnelson Feb 21, 2024

Choose a reason for hiding this comment

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

Yeah, that looks fine.

Would it make sense to add a static helper for finding the enclosing (non-disabled) FrameElement? In that case the findClosestRecursively could be replaced by e.g. FrameElement.findEnclosingFrame(element) and the check would become if (isUnsafe || isStream || frame) {.

Choose a reason for hiding this comment

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

For the purposes of code reuse, I can see two additional utility methods that would be helpful:

  • FrameElement.findEnclosingFrame(element) would walk up the tree to find the enclosing frame (if any)
  • FrameElement.findTargetFrame(element) would expose #findFrameElement – given an element it would identify which frame should be targeted – for use with links/forms that target frames for navigation

I think these would bring general utility to downstream consumers of Turbo – both of these cases have come up in our use of Frames to implement modals.

The `FrameElement.getElementById(id: string)` static method unifies the
internal logic to locate a `<turbo-frame>` element by its `[id]`, while
providing that consistent behavior to consumer applications.
@seanpdoyle seanpdoyle force-pushed the frame-element-get-by-element-id branch from b750a8f to 3f3d99c Compare March 29, 2024 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants