Add support for foreign Iframe context by Srijan619 · Pull Request #3097 · flatpickr/flatpickr · GitHub
Skip to content

Add support for foreign Iframe context - #3097

Open
Srijan619 wants to merge 13 commits into
flatpickr:masterfrom
Srijan619:master
Open

Srijan619 wants to merge 13 commits into
flatpickr:masterfrom
Srijan619:master

Conversation

@Srijan619

Copy link
Copy Markdown

#3096

Check and allow instantiating flatpickr in iframe context. Currently it is not possible as it directly checks against main window context. This PR adds support with backwards compatibility if in non-iframe context

@Srijan619 Srijan619 mentioned this pull request Jun 3, 2025
positionCalendar() measured scroll offset/viewport size/input bounds
unconditionally from the local window, while calendar elements are
created via createElement()'s doc() which may resolve to window.top
when embedded cross-realm (e.g. inside an iframe). When appendTo
points at a different realm than the input, this mismatch produced
wrong (including negative) top/left/right values. Now derives the
render window/document from the actual resolved appendTo target and
translates the input's bounding rect into that realm via
window.frameElement when they differ.

Also binds the outside-click-to-close listener on the appendTo
target's document in addition to the local one, since clicks in a
different-realm calendar never reached a listener bound only locally.

@colinli007 colinli007 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for working on iframe support. I tested this branch in Chrome 154 against master,
and it does fix #3096 (library in an iframe, element in the parent page). But it also
changes a case that works today:

flatpickr loaded and used normally inside a same-origin iframe (e.g. Storybook, CMS or
block editors): getDocument() resolves window.top.document, so the calendar is created
in and mounted to the top page instead of the iframe, and mousedown inside the iframe no
longer closes it.

master this PR
calendar mounted in iframe's own document top document
outside click inside iframe closes yes no

It also doesn't cover #3138 (library in the main window, element in an iframe or popup):
_flatpickr still returns no instance there, same as master. Minor: npm run fmt:check
fails on this branch, and the diff includes a version bump and a prepare script.

I've opened #3143 with a different approach that resolves the document from
element.ownerDocument per instance instead of window.top. It covers #3096 and #3138
without changing the same-origin iframe case; there's a full comparison table there.
Happy to hear if your setup has a case it misses.

— Colin

This branch has not been deployed

No deployments
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.

3 participants