@@ -81,9 +81,9 @@ interface CustomProps<LinkOptions> {
81
81
openShareDialogOnClick ?: boolean ;
82
82
opts : LinkOptions ;
83
83
/**
84
- * URL of the shared page
84
+ * URL of the shared page, can be an async function that resolves a URL
85
85
*/
86
- url : string ;
86
+ url : string | ( ( ) => Promise < string > ) ;
87
87
style ?: React . CSSProperties ;
88
88
windowWidth ?: number ;
89
89
windowHeight ?: number ;
@@ -92,7 +92,8 @@ interface CustomProps<LinkOptions> {
92
92
* Takes a function that returns a Promise to be fulfilled before calling
93
93
* `onClick`. If you do not return promise, `onClick` is called immediately.
94
94
*/
95
- beforeOnClick ?: ( ) => Promise < void > | void ;
95
+ beforeOnClick ?: ( ) => void | Promise < void > ;
96
+
96
97
/**
97
98
* Takes a function to be called after closing share dialog.
98
99
*/
@@ -132,20 +133,22 @@ export default class ShareButton<LinkOptions> extends Component<Props<LinkOption
132
133
windowOpen ( link , windowConfig , onShareWindowClose ) ;
133
134
} ;
134
135
135
- handleClick = async (
136
- event : React . MouseEvent < HTMLButtonElement > ,
137
- ignoreBeforeOnClick = false ,
138
- ) : Promise < void > => {
136
+ handleClick = async ( event : React . MouseEvent < HTMLButtonElement > ) : Promise < void > => {
139
137
const {
140
138
beforeOnClick,
141
139
disabled,
142
140
networkLink,
143
141
onClick,
144
- url,
145
142
openShareDialogOnClick,
146
143
opts,
147
144
} = this . props ;
148
145
146
+ let url = this . props . url ;
147
+
148
+ if ( typeof url == 'function' ) {
149
+ url = await url ( ) ;
150
+ }
151
+
149
152
const link = networkLink ( url , opts ) ;
150
153
151
154
if ( disabled ) {
@@ -154,19 +157,12 @@ export default class ShareButton<LinkOptions> extends Component<Props<LinkOption
154
157
155
158
event . preventDefault ( ) ;
156
159
157
- if ( beforeOnClick && ! ignoreBeforeOnClick ) {
158
- // Make the event object usuable in the following handleClick call
159
- event . persist ( ) ;
160
-
160
+ if ( beforeOnClick ) {
161
161
const returnVal = beforeOnClick ( ) ;
162
162
163
163
if ( isPromise ( returnVal ) ) {
164
164
await returnVal ;
165
165
}
166
-
167
- // beforeOnClick could change our props, so let's re-run handleClick
168
- setTimeout ( ( ) => this . handleClick ( event , true ) ) ;
169
- return ;
170
166
}
171
167
172
168
if ( openShareDialogOnClick ) {
0 commit comments