@planningcenter/topbar 11.0.1 → 11.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -686,6 +686,13 @@
686
686
  };
687
687
 
688
688
  const capitalize = s => s.charAt(0).toUpperCase() + s.slice(1);
689
+ const RESOLVED_COLOR_MODE_ATTRIBUTE = "data-resolved-color-mode";
690
+ function resolveColorMode(colorMode) {
691
+ if (colorMode === "system") {
692
+ return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
693
+ }
694
+ return colorMode;
695
+ }
689
696
  const debounce = (func, wait) => {
690
697
  let timeout;
691
698
  return function (...args) {
@@ -766,6 +773,58 @@
766
773
  return storedValue;
767
774
  };
768
775
 
776
+ function useMediaQuery(query) {
777
+ const getMatches = query => {
778
+ // Prevents SSR issues
779
+ if (typeof window !== "undefined") {
780
+ return window.matchMedia(query).matches;
781
+ }
782
+ return false;
783
+ };
784
+ const [matches, setMatches] = React.useState(getMatches(query));
785
+ function handleChange() {
786
+ setMatches(getMatches(query));
787
+ }
788
+ React.useEffect(() => {
789
+ const matchMedia = window.matchMedia(query);
790
+ // Triggered at the first client-side load and if query changes
791
+ handleChange();
792
+ // Listen matchMedia
793
+ if (matchMedia.addListener) {
794
+ matchMedia.addListener(handleChange);
795
+ } else {
796
+ matchMedia.addEventListener("change", handleChange);
797
+ }
798
+ return () => {
799
+ if (matchMedia.removeListener) {
800
+ matchMedia.removeListener(handleChange);
801
+ } else {
802
+ matchMedia.removeEventListener("change", handleChange);
803
+ }
804
+ };
805
+ }, [query]);
806
+ return matches;
807
+ }
808
+
809
+ function createStore(initialState) {
810
+ let currentState = initialState;
811
+ const listeners = new Set();
812
+ return {
813
+ getState: () => currentState,
814
+ setState: newState => {
815
+ currentState = newState;
816
+ listeners.forEach(listener => listener(currentState));
817
+ },
818
+ subscribe: listener => {
819
+ listeners.add(listener);
820
+ return () => listeners.delete(listener);
821
+ }
822
+ };
823
+ }
824
+ const store$1 = createStore({});
825
+
826
+ const useStore = (selector = state => state) => shim.useSyncExternalStore(store$1.subscribe, () => selector(store$1.getState()));
827
+
769
828
  const useTopbarAPI = ({
770
829
  configuredFetch,
771
830
  env
@@ -792,23 +851,6 @@
792
851
  return resp;
793
852
  };
794
853
 
795
- function createStore(initialState) {
796
- let currentState = initialState;
797
- const listeners = new Set();
798
- return {
799
- getState: () => currentState,
800
- setState: newState => {
801
- currentState = newState;
802
- listeners.forEach(listener => listener(currentState));
803
- },
804
- subscribe: listener => {
805
- listeners.add(listener);
806
- return () => listeners.delete(listener);
807
- }
808
- };
809
- }
810
- const store$1 = createStore({});
811
-
812
854
  const AppsProvider = ({
813
855
  configuredFetch = defaultFetch,
814
856
  enableJoltLogging,
@@ -828,6 +870,10 @@
828
870
  const hasConnectedPeople = (_b = (_a = topbarResponse === null || topbarResponse === void 0 ? void 0 : topbarResponse.data) === null || _a === void 0 ? void 0 : _a.attributes) === null || _b === void 0 ? void 0 : _b.has_connected_people;
829
871
  const mfaConfigured = !!((_d = (_c = topbarResponse === null || topbarResponse === void 0 ? void 0 : topbarResponse.data) === null || _c === void 0 ? void 0 : _c.attributes) === null || _d === void 0 ? void 0 : _d.mfa_configured);
830
872
  const colorMode = (_f = (_e = topbarResponse === null || topbarResponse === void 0 ? void 0 : topbarResponse.data) === null || _e === void 0 ? void 0 : _e.attributes) === null || _f === void 0 ? void 0 : _f.color_mode;
873
+ const {
874
+ colorMode: currentColorMode
875
+ } = useStore();
876
+ const prefersDark = useMediaQuery("(prefers-color-scheme: dark)");
831
877
  const name = (_h = (_g = topbarResponse === null || topbarResponse === void 0 ? void 0 : topbarResponse.data) === null || _g === void 0 ? void 0 : _g.attributes) === null || _h === void 0 ? void 0 : _h.name;
832
878
  const notificationsEnabled = !window.notificationsDisabled;
833
879
  const organizationTimeZone = ((_k = (_j = topbarResponse === null || topbarResponse === void 0 ? void 0 : topbarResponse.data) === null || _j === void 0 ? void 0 : _j.attributes) === null || _k === void 0 ? void 0 : _k.org_time_zone) || "America/Los_Angeles";
@@ -879,6 +925,16 @@
879
925
  enabled: true,
880
926
  joltChannel: notificationsJoltChannel
881
927
  });
928
+ React.useLayoutEffect(() => {
929
+ if (colorMode) {
930
+ document.documentElement.setAttribute(RESOLVED_COLOR_MODE_ATTRIBUTE, resolveColorMode(colorMode));
931
+ }
932
+ }, [colorMode]);
933
+ React.useEffect(() => {
934
+ if (currentColorMode === "system") {
935
+ document.documentElement.setAttribute(RESOLVED_COLOR_MODE_ATTRIBUTE, prefersDark ? "dark" : "light");
936
+ }
937
+ }, [currentColorMode, prefersDark]);
882
938
  React.useEffect(() => {
883
939
  const state = store$1.getState();
884
940
  store$1.setState({
@@ -2047,8 +2103,6 @@
2047
2103
  }, [routes, app]);
2048
2104
  };
2049
2105
 
2050
- const useStore = (selector = state => state) => shim.useSyncExternalStore(store$1.subscribe, () => selector(store$1.getState()));
2051
-
2052
2106
  const useUpdateStore = () => React.useCallback(updates => {
2053
2107
  const currentState = store$1.getState();
2054
2108
  store$1.setState({
@@ -2956,6 +3010,7 @@
2956
3010
  colorMode
2957
3011
  });
2958
3012
  document.documentElement.setAttribute("data-color-mode", colorMode);
3013
+ document.documentElement.setAttribute(RESOLVED_COLOR_MODE_ATTRIBUTE, resolveColorMode(colorMode));
2959
3014
  updateColorMode(colorMode);
2960
3015
  }
2961
3016
  }), /*#__PURE__*/React.createElement(ColorModeIcon, {
@@ -4390,7 +4445,7 @@
4390
4445
  document.body.style.overflow = "hidden";
4391
4446
  window.scrollTo(0, 0);
4392
4447
  } else {
4393
- document.body.style.overflow = "auto";
4448
+ document.body.style.overflow = "";
4394
4449
  }
4395
4450
  }, [open]);
4396
4451
  // Listen for external close requests from store