Skip to content

Commit

Permalink
✨ useCssVar実装
Browse files Browse the repository at this point in the history
  • Loading branch information
takuma-ru committed May 9, 2024
1 parent e02dcce commit 6e05904
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/core/src/hooks/useCssVar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// eslint-disable-next-line ts/ban-types
type CssVarName = "bottom" | "movementAmountY" | "snapPointPosition" | String;

interface UseCssVarParameter {
scopeName: string;
}

interface SetCssVarParameter {
name: CssVarName;
value: string;
fallback?: (name: CssVarName, value: string) => void;
}

export const useCssVar = ({ scopeName }: UseCssVarParameter) => {
const getCssVar = (name: CssVarName) => {
return getComputedStyle(document.documentElement).getPropertyValue(`--${scopeName}-${name}`);
};

const setCssVar = ({ name, value, fallback }: SetCssVarParameter) => {
document.documentElement.style.setProperty(`--${scopeName}-${name}`, value);

Check failure on line 20 in packages/core/src/hooks/useCssVar.ts

View workflow job for this annotation

GitHub Actions / eslint-check

Mixed spaces and tabs

fallback?.(
name,
getCssVar(name),
);
};

return {
setCssVar,
getCssVar,
};
};

0 comments on commit 6e05904

Please sign in to comment.