use-calculate-sheet-right.ts


Overview

The use-calculate-sheet-right.ts file defines a custom React hook useCalculateSheetRight that calculates a CSS class string to position an element from the right edge of the viewport dynamically based on the current width of the <body> element.

This hook leverages the useSize hook from the ahooks library to observe DOM size changes and returns a responsive CSS class string to be used in styling components, particularly for layout or positioning of side sheets, panels, or similar UI elements.


Detailed Description

Function: useCalculateSheetRight

export function useCalculateSheetRight(): string

Purpose

useCalculateSheetRight determines the CSS class to apply a right offset to an element, adapting the offset according to the current width of the browser's body element.

How it works

Parameters

This function takes no parameters.

Returns

Usage Example

import React from 'react';
import { useCalculateSheetRight } from './use-calculate-sheet-right';

function SideSheet() {
  const rightClass = useCalculateSheetRight();

  return (
    <div className={`fixed top-0 ${rightClass} w-80 h-full bg-white shadow-lg`}>
      {/* Side sheet content */}
    </div>
  );
}

In this example, the side sheet's position from the right edge changes responsively based on the body width.


Implementation Details


Interaction with the System


Mermaid Diagram

flowchart TD
    A[useCalculateSheetRight] --> B[useSize(document.querySelector('body'))]
    B --> C{size}
    C -->|defined| D[bodyWidth = size.width]
    C -->|undefined| E[bodyWidth = 0]
    D --> F{bodyWidth > 1800?}
    E --> F
    F -->|true| G[return 'right-[620px]']
    F -->|false| H[return 'right-1/3']

Summary

This simple yet effective utility supports responsive UI design by abstracting position calculations into a reusable hook.