Skip to content

Commit 7ebb018

Browse files
authored
Add show_delay prop to spinner (#701)
* Add delay prop to spinner * Rename debounce -> hide_delay
1 parent 6fbb8f5 commit 7ebb018

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

docs/content/migration-guide.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ the [Bootstrap grid docs](https://getbootstrap.com/docs/5.0/layout/grid/#row-col
160160
### Select
161161
- <span class="badge bg-danger">Breaking</span> Dropped `bs_size` property. Use `size` instead.
162162

163+
### Spinner
164+
- `Spinner` has a new prop `show_delay` which can be used to control how long a component should be loading for before the spinner shows.
165+
- <span class="badge bg-danger">Breaking</span> `debounce` prop has been renamed to `hide_delay` to mirror `show_delay`. This prop adds a delay to the spinner being hidden once the child component has finished loading.
166+
163167
### Switch <span class="badge bg-success">New</span>
164168
- New `Switch` component. See the documentation [here](/docs/components/input)
165169

src/components/spinner/Spinner.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,50 @@ const Spinner = props => {
2323
fullscreenClassName,
2424
fullscreen_class_name,
2525
fullscreen_style,
26-
debounce,
26+
hide_delay,
27+
show_delay,
2728
show_initially,
2829
type,
2930
...otherProps
3031
} = props;
3132

3233
const [showSpinner, setShowSpinner] = useState(show_initially);
33-
const timer = useRef();
34+
const dismissTimer = useRef();
35+
const showTimer = useRef();
3436

3537
useEffect(() => {
3638
if (loading_state) {
37-
if (timer.current) {
38-
clearTimeout(timer.current);
39-
}
40-
if (loading_state.is_loading && !showSpinner) {
41-
setShowSpinner(true);
42-
} else if (!loading_state.is_loading && showSpinner) {
43-
timer.current = setTimeout(() => setShowSpinner(false), debounce);
39+
if (loading_state.is_loading) {
40+
// if component is currently loading and there's a dismiss timer active
41+
// we need to clear it.
42+
if (dismissTimer.current) {
43+
dismissTimer.current = clearTimeout(dismissTimer.current);
44+
}
45+
// if component is currently loading but the spinner is not showing and
46+
// there is no timer set to show, then set a timeout to show
47+
if (!showSpinner && !showTimer.current) {
48+
showTimer.current = setTimeout(() => {
49+
setShowSpinner(true);
50+
showTimer.current = null;
51+
}, show_delay);
52+
}
53+
} else {
54+
// if component is not currently loading and there's a show timer
55+
// active we need to clear it
56+
if (showTimer.current) {
57+
showTimer.current = clearTimeout(showTimer.current);
58+
}
59+
// if component is not currently loading and the spinner is showing and
60+
// there's no timer set to dismiss it, then set a timeout to hide it
61+
if (showSpinner && !dismissTimer.current) {
62+
dismissTimer.current = setTimeout(() => {
63+
setShowSpinner(false);
64+
dismissTimer.current = null;
65+
}, hide_delay);
66+
}
4467
}
4568
}
46-
}, [loading_state]);
69+
}, [hide_delay, show_delay, loading_state]);
4770

4871
const isBootstrapColor = bootstrapColors.has(color);
4972

@@ -131,7 +154,8 @@ const Spinner = props => {
131154
Spinner._dashprivate_isLoadingComponent = true;
132155

133156
Spinner.defaultProps = {
134-
debounce: 0,
157+
hide_delay: 0,
158+
show_delay: 0,
135159
show_initially: true,
136160
type: 'border'
137161
};
@@ -213,7 +237,13 @@ Spinner.propTypes = {
213237
* When using the spinner as a loading spinner, add a time delay (in ms) to
214238
* the spinner being removed to prevent flickering.
215239
*/
216-
debounce: PropTypes.number,
240+
hide_delay: PropTypes.number,
241+
242+
/**
243+
* When using the spinner as a loading spinner, add a time delay (in ms) to
244+
* the spinner being shown after the loading_state is set to true.
245+
*/
246+
show_delay: PropTypes.number,
217247

218248
/**
219249
* Whether the Spinner should show on app start-up before the loading state

0 commit comments

Comments
 (0)