Skip to content
This repository was archived by the owner on Apr 7, 2025. It is now read-only.

Commit aa0b5bf

Browse files
Merge pull request #243 from CarinaChenot/add-popup-customization
Add popup customization
2 parents 3d9cc80 + 841f591 commit aa0b5bf

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ The `url` is the only property required for all networks.
8787

8888
###### General properties
8989

90-
Name | Data Type | Description
91-
-------------- | ---------- | -----------
92-
`tag` | String | HTML tag used to render the network component. Default to "a" tag.
90+
| Name | Data Type | Description |
91+
|----------------|-----------| -------------------------------------------------------------------|
92+
| `tag` | String | HTML tag used to render the network component. Default to "a" tag. |
93+
| `popup.width` | Number | Custom width of the popup window. Default to 626px. |
94+
| `popup.height` | Number | Custom height of the popup window. Default to 426px.       |
9395

9496
###### Network properties
9597

@@ -201,6 +203,13 @@ modules: [
201203
]
202204
```
203205

206+
## Customizing the popup window size
207+
208+
If needed, you can set a custom width and height for the popup window:
209+
```html
210+
<ShareNetwork :popup="{width: 400, height: 200}" />
211+
```
212+
204213
## Feature request
205214
Feel free to open an issue to ask for a new social network support.
206215

src/share-network.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,26 @@ export default {
8181
tag: {
8282
type: String,
8383
default: 'a'
84+
},
85+
86+
/**
87+
* Properties to configure the popup window.
88+
*/
89+
popup: {
90+
type: Object,
91+
default: () => ({
92+
width: 626,
93+
height: 436
94+
})
8495
}
8596
},
8697

8798
data () {
8899
return {
89-
popup: {
90-
width: 626,
91-
height: 436,
92-
top: 0,
93-
left: 0,
94-
window: undefined,
95-
interval: null
96-
}
100+
popupTop: 0,
101+
popupLeft: 0,
102+
popupWindow: undefined,
103+
popupInterval: null
97104
}
98105
},
99106

@@ -191,8 +198,8 @@ export default {
191198
const height = $window.innerHeight || (document.documentElement.clientHeight || $window.screenY)
192199
const systemZoom = width / $window.screen.availWidth
193200

194-
this.popup.left = (width - this.popup.width) / 2 / systemZoom + ($window.screenLeft !== undefined ? $window.screenLeft : $window.screenX)
195-
this.popup.top = (height - this.popup.height) / 2 / systemZoom + ($window.screenTop !== undefined ? $window.screenTop : $window.screenY)
201+
this.popupLeft = (width - this.popup.width) / 2 / systemZoom + ($window.screenLeft !== undefined ? $window.screenLeft : $window.screenX)
202+
this.popupTop = (height - this.popup.height) / 2 / systemZoom + ($window.screenTop !== undefined ? $window.screenTop : $window.screenY)
196203
},
197204

198205
/**
@@ -202,37 +209,37 @@ export default {
202209
this.resizePopup()
203210

204211
// If a popup window already exist, we close it and trigger a change event.
205-
if (this.popup.window && this.popup.interval) {
206-
clearInterval(this.popup.interval)
212+
if (this.popupWindow && this.popupInterval) {
213+
clearInterval(this.popupInterval)
207214

208215
// Force close (for Facebook)
209-
this.popup.window.close()
216+
this.popupWindow.close()
210217

211218
this.emit('change')
212219
}
213220

214-
this.popup.window = $window.open(
221+
this.popupWindow = $window.open(
215222
this.shareLink,
216223
'sharer',
217224
',height=' + this.popup.height +
218225
',width=' + this.popup.width +
219-
',left=' + this.popup.left +
220-
',top=' + this.popup.top +
221-
',screenX=' + this.popup.left +
222-
',screenY=' + this.popup.top
226+
',left=' + this.popupLeft +
227+
',top=' + this.popupTop +
228+
',screenX=' + this.popupLeft +
229+
',screenY=' + this.popupTop
223230
)
224231

225232
// If popup are prevented (AdBlocker, Mobile App context..), popup.window stays undefined and we can't display it
226-
if (!this.popup.window) return
233+
if (!this.popupWindow) return
227234

228-
this.popup.window.focus()
235+
this.popupWindow.focus()
229236

230237
// Create an interval to detect popup closing event
231-
this.popup.interval = setInterval(() => {
232-
if (!this.popup.window || this.popup.window.closed) {
233-
clearInterval(this.popup.interval)
238+
this.popupInterval = setInterval(() => {
239+
if (!this.popupWindow || this.popupWindow.closed) {
240+
clearInterval(this.popupInterval)
234241

235-
this.popup.window = null
242+
this.popupWindow = null
236243

237244
this.emit('close')
238245
}

tests/test.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,27 @@ describe('SocialSharing', () => {
8080
// default width popup = 626
8181
// default height popup = 436
8282

83-
expect(vm.popup.left).toBe(187) // 1000 / 2 - 626 / 2 = 187
84-
expect(vm.popup.top).toBe(132) // 700 / 2 - 436 / 2 = 132
83+
expect(vm.popupLeft).toBe(187) // 1000 / 2 - 626 / 2 = 187
84+
expect(vm.popupTop).toBe(132) // 700 / 2 - 436 / 2 = 132
85+
})
86+
87+
// Sets correct popup size
88+
it('sets correct popup size', () => {
89+
const vm = mountShareNetwork({
90+
propsData: {
91+
network: 'facebook',
92+
url: 'http://vuejs.org/',
93+
title: 'The Progressive JavaScript Framework',
94+
popup: {
95+
height: 100,
96+
width: 100
97+
}
98+
}
99+
}).vm
100+
101+
expect(vm.popup.height).toBe(100)
102+
expect(vm.popup.width).toBe(100)
103+
expect(vm.popupTop).toBe(0)
85104
})
86105

87106
it('create component with a link tag', () => {

0 commit comments

Comments
 (0)