unit-action-width.js 1.1 KB

123456789101112131415161718192021222324252627
  1. import { createContext, use } from 'react';
  2. import { Platform, useWindowDimensions } from 'react-native';
  3. import { getBreakpoint } from 'app/lib/util';
  4. /**
  5. * How unit action buttons (menu-items/unit.js) size, set by `UnitActions`:
  6. * - 'fill' (default): the button fills its slot — card grids, the flush
  7. * mobile rows' half-width actions.
  8. * - 'compact-mobile': below `sm` the button hugs its label (profile rows on
  9. * phones: avatar, name, then a compact button on the right); from `sm` up
  10. * it fills, as in the card grid.
  11. *
  12. * Its own module: units/helpers.js imports the menu-item map through
  13. * customization/functions, so the menu item can't import helpers back.
  14. */
  15. export const UnitActionWidthContext = createContext('fill');
  16. /**
  17. * True when the unit action button should hug its label. Native only: on
  18. * web, `fill` in the row's auto-sized slot already hugs (CSS shrink-to-fit),
  19. * while the native layout engine widens it.
  20. */
  21. export function useUnitActionHugs() {
  22. const mode = use(UnitActionWidthContext);
  23. const { width } = useWindowDimensions();
  24. return mode === 'compact-mobile' && Platform.OS !== 'web' && !getBreakpoint(width);
  25. }