Skip to content

Commit 6b68f37

Browse files
author
Matej Lednicky
committed
fix(DIST-366): Display correct icon when popup is opened automatically
When opening popup via behavioral triggers (for popover and side panel popup types) display icon to correctly indicate current state - opened, loading, closed.
1 parent 6c373df commit 6b68f37

7 files changed

Lines changed: 49 additions & 9 deletions

File tree

demo/behavioral-api/load-api.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
<h1>This popup opens on page load (via API)</h1>
99
<p>If you see this you very likely already closed the popup.</p>
1010
<p>If the popup did not open automatically something is broken.</p>
11+
<p>Popover embed does not have a close button by default and needs to be explicitly provided when embedding via API. <a href="../popup-api.html">See popup API demo for details.</a></p>
1112

1213
<script type="text/javascript" src="../embed.js"></script>
1314
<script>
1415
window.typeformEmbed.makePopup(
1516
'../form/mock.html#foobar=hello',
1617
{
17-
mode: 'popup',
18+
mode: 'popover',
1819
open: 'load',
20+
width: 400,
21+
height: 550,
1922
}
2023
)
2124
</script>

demo/behavioral-code/load-code.html

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
<h1>This popup opens on page load (via embed code)</h1>
99
<p>If you see this you very likely already closed the popup.</p>
1010
<p>If the popup did not open automatically something is broken.</p>
11+
1112
<a
12-
class="typeform-share"
13+
class="typeform-share button"
1314
href="../form/mock.html#foobar=hello"
14-
data-mode="popup"
15+
data-mode="popover"
1516
data-open="load"
16-
data-open-value="1"
17+
data-width="400"
18+
data-height="550"
19+
style="width:54px;height:54px;position:fixed;right:26px;bottom:26px;border-radius:50%;display:flex;align-items:center;justify-content:center;cursor:pointer;background:#647;overflow:hidden;line-height:0;"
1720
target="_blank"
1821
>
19-
Open the popup manually instead
22+
<span class="icon">
23+
<svg width='24' height='24' viewBox='0 0 24 24' fill='none' xmlns='http://www.w3.org/2000/svg' style="margin-top:10px;"><path d='M21 0H0V9L10.5743 24V16.5H21C22.6567 16.5 24 15.1567 24 13.5V3C24 1.34325 22.6567 0 21 0ZM7.5 9.75C6.672 9.75 6 9.07875 6 8.25C6 7.42125 6.672 6.75 7.5 6.75C8.328 6.75 9 7.42125 9 8.25C9 9.07875 8.328 9.75 7.5 9.75ZM12.75 9.75C11.922 9.75 11.25 9.07875 11.25 8.25C11.25 7.42125 11.922 6.75 12.75 6.75C13.578 6.75 14.25 7.42125 14.25 8.25C14.25 9.07875 13.578 9.75 12.75 9.75ZM18 9.75C17.172 9.75 16.5 9.07875 16.5 8.25C16.5 7.42125 17.172 6.75 18 6.75C18.828 6.75 19.5 7.42125 19.5 8.25C19.5 9.07875 18.828 9.75 18 9.75Z' fill='white' /></svg>
24+
</span>
2025
</a>
2126

2227
<script type="text/javascript" src="../embed.js"></script>

e2e/spec/functional/behavioral-popup.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,29 @@ Object.keys(pages).forEach(pageSuffix => {
9090
})
9191
})
9292
})
93+
94+
describe('Open: load (via embed code)', () => {
95+
before(() => {
96+
open(getPageUrl('load', '-code'))
97+
})
98+
99+
it('should display close button', () => {
100+
cy.get('.typeform-share > .icon > img[data-qa="popup-close-button"]').should('be.visible')
101+
cy.get('.typeform-share > .icon > svg').should('not.be.visible')
102+
})
103+
104+
describe('when popup is closed', () => {
105+
before(() => {
106+
cy.get('.typeform-share').click()
107+
})
108+
109+
it('should display the popup', () => {
110+
cy.get(IFRAME_SELECTOR).should('not.be.visible')
111+
})
112+
113+
it('should display original icon', () => {
114+
cy.get('.typeform-share > .icon > img[data-qa="popup-close-button"]').should('not.be.visible')
115+
cy.get('.typeform-share > .icon > svg').should('be.visible')
116+
})
117+
})
118+
})

src/core/make-popup.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const renderComponent = (params, options) => {
113113
}
114114
}
115115

116-
export default function makePopup (url, userOptions) {
116+
export default function makePopup (url, userOptions, element) {
117117
window.addEventListener('message', getPostMessageHandler('form-ready', userOptions.onReady))
118118
window.addEventListener('message', getPostMessageHandler('form-closed', userOptions.onClose))
119119

@@ -138,8 +138,9 @@ export default function makePopup (url, userOptions) {
138138
options.container.appendChild(domNode)
139139

140140
const popup = {
141+
element,
141142
open (event) {
142-
const { currentTarget } = event || {}
143+
const currentTarget = event && event.currentTarget ? event.currentTarget : this.element
143144
const currentUrl = currentTarget && currentTarget.href ? currentTarget.href : url
144145
const icon = currentTarget && currentTarget.querySelector('span.icon')
145146
const params = {

src/core/make-popup.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ describe('makePopup', () => {
9494
expect(renderMock).toHaveBeenCalledTimes(0)
9595
})
9696

97+
it('attaches the element to popup object', () => {
98+
const mockElement = document.createElement('test')
99+
expect(makePopup('', {}, mockElement).element).toBe(mockElement)
100+
})
101+
97102
it(`renders a Popup component on desktop devices when 'width' option is a valid number`, () => {
98103
const options = { open: 'load', width: 650 }
99104

src/embed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const initializePopup = (element) => {
2626
data.container = element.parentNode
2727
}
2828

29-
const popup = makePopup(url, data)
29+
const popup = makePopup(url, data, element)
3030

3131
element.onclick = (event) => {
3232
event.stopPropagation()

src/embed.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('Embed', () => {
4646
it('initialises popup elements', () => {
4747
expect(makePopupMock).toHaveBeenCalledTimes(1)
4848
expect(sanitizePopupAttributesMock).toHaveBeenCalledTimes(1)
49-
expect(makePopupMock).toBeCalledWith(POPUP_URL, POPUP_ATTR)
49+
expect(makePopupMock).toBeCalledWith(POPUP_URL, POPUP_ATTR, document.querySelector('.typeform-share'))
5050
})
5151

5252
it('initialises widget elements', () => {

0 commit comments

Comments
 (0)