useFocusPositionOnLayout.ts 579 B

1234567891011121314151617181920
  1. import usePrevious from 'react-use/lib/usePrevious';
  2. import { Config } from './layout';
  3. import { NodeDatum } from './types';
  4. export function useFocusPositionOnLayout(config: Config, nodes: NodeDatum[], focusedNodeId: string | undefined) {
  5. const prevLayoutGrid = usePrevious(config.gridLayout);
  6. let focusPosition;
  7. if (prevLayoutGrid === true && !config.gridLayout && focusedNodeId) {
  8. const node = nodes.find((n) => n.id === focusedNodeId);
  9. if (node) {
  10. focusPosition = {
  11. x: -node.x!,
  12. y: -node.y!,
  13. };
  14. }
  15. }
  16. return focusPosition;
  17. }