• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

terrestris / react-geo / 19291472123

12 Nov 2025 08:44AM UTC coverage: 67.586% (-0.1%) from 67.733%
19291472123

push

github

web-flow
Merge pull request #4442 from terrestris/geolocation-button-togglegroup

fix: improve ToggleGroup integration and context detection for GeoLocationButton

674 of 1086 branches covered (62.06%)

Branch coverage included in aggregate %.

4 of 11 new or added lines in 1 file covered. (36.36%)

1238 of 1743 relevant lines covered (71.03%)

13.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.0
/src/Button/GeoLocationButton/GeoLocationButton.tsx
1
import React, { FC, useState } from 'react';
2

3
import {
4
  type GeoLocation,
5
  useGeoLocation
6
} from '@terrestris/react-util/dist/Hooks/useGeoLocation/useGeoLocation';
7

8
import { CSS_PREFIX } from '../../constants';
9
import ToggleButton, { ToggleButtonProps } from '../ToggleButton/ToggleButton';
10

11
interface OwnProps {
12
  trackingOptions?: PositionOptions;
13
  /**
14
   * Will be called if geolocation fails.
15
   */
16
  onError?: () => void;
17
  /**
18
   * Will be called when position changes. Receives an object with the properties
19
   * position, accuracy, heading and speed
20
   */
21
  onGeoLocationChange?: (geolocation: GeoLocation) => void;
22
  /**
23
   * Whether to show a map marker at the current position.
24
   */
25
  showMarker?: boolean;
26
  /**
27
   * Whether to follow the current position.
28
   */
29
  follow?: boolean;
30
  /**
31
   * Enable tracking of GeoLocations
32
   */
33
  enableTracking?: boolean;
34
  /**
35
   * Boolean callback for when the button is toggled outside of a ToggleGroup
36
   */
37
  onPressedChange?: (pressed: boolean) => void;
38
}
39

40
export type GeoLocationButtonProps = OwnProps & Partial<ToggleButtonProps>;
41

42
export const GeoLocationButton: FC<GeoLocationButtonProps> = ({
1✔
43
  className,
44
  enableTracking = false,
2✔
45
  follow = false,
14✔
46
  onChange,
47
  onError = () => undefined,
✔
48
  onGeoLocationChange = () => undefined,
✔
49
  pressed,
50
  showMarker = true,
1✔
51
  trackingOptions,
52
  onPressedChange,
53
  ...passThroughProps
54
}) => {
55
  const [internalPressed, setInternalPressed] = useState(false);
14✔
56
  const isControlled = typeof pressed === 'boolean';
14✔
57
  const effectivePressed = isControlled ? !!pressed : internalPressed;
14✔
58

59
  useGeoLocation({
14✔
60
    active: effectivePressed,
61
    enableTracking: effectivePressed && enableTracking,
20✔
62
    follow,
63
    onError,
64
    onGeoLocationChange,
65
    showMarker,
66
    trackingOptions
67
  });
68

69
  const finalClassName = className
14!
70
    ? `${className} ${CSS_PREFIX}geolocationbutton`
71
    : `${CSS_PREFIX}geolocationbutton`;
72

73
  const handleChange: ToggleButtonProps['onChange'] = (evt, value) => {
14✔
NEW
74
    if (!isControlled) {
×
NEW
75
      setInternalPressed(prevPressed => {
×
NEW
76
        const nextPressed = !prevPressed;
×
NEW
77
        onPressedChange?.(nextPressed);
×
NEW
78
        return nextPressed;
×
79
      });
80
    } else {
NEW
81
      onPressedChange?.(!effectivePressed);
×
82
    }
NEW
83
    onChange?.(evt, value);
×
84
  };
85

86
  return (
14✔
87
    <ToggleButton
88
      pressed={effectivePressed}
89
      className={finalClassName}
90
      onChange={handleChange}
91
      {...passThroughProps}
92
    />
93
  );
94
};
95

96
export default GeoLocationButton;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc