javascript 获取当前材质UI断点名称

2exbekwf  于 2023-10-14  发布在  Java
关注(0)|答案(5)|浏览(84)

我正在搜索一个MUI函数“MaterialUIGiveMeCurrentBreakPointName“,它允许我在这样的组件中执行一个操作:

  1. const currentBreakPointName = MaterialUIGiveMeCurrentBreakPointName()
  2. if(currentBreakPointName === 'myCustomBreakPointName') {
  3. // do stuff
  4. }

你能帮帮我吗?

vwoqyblh

vwoqyblh1#

目前还没有这样的函数可以返回当前断点名称,但是你可以使用useMediaQuery钩子来实现。

参考https://mui.com/material-ui/react-use-media-query/
Working codesandboxhttps://codesandbox.io/s/themehelper-demo-material-ui-forked-h2grmr?file=/demo.tsx

  1. const theme = useTheme();
  2. const greaterThanMid = useMediaQuery(theme.breakpoints.up("md"));
  3. const smallToMid = useMediaQuery(theme.breakpoints.between("sm", "md"));
  4. const lessThanSmall = useMediaQuery(theme.breakpoints.down("sm"));
  5. if (greaterThanMid) {
  6. console.log("Greater than mid");
  7. } else if (smallToMid) {
  8. console.log("Between small to mid");
  9. } else if (lessThanSmall) {
  10. console.log("Less than small");
  11. }

展开查看全部
kse8i1jr

kse8i1jr2#

这里有一个定制的钩子,会适合你的需要。

  1. import {
  2. Breakpoint,
  3. Theme,
  4. useMediaQuery,
  5. useTheme
  6. } from '@mui/material/styles'
  7. /**
  8. * taken from https://material-ui.com/components/use-media-query/#migrating-from-withwidth
  9. *
  10. * Be careful using this hook. It only works because the number of
  11. * breakpoints in theme is static. It will break once you change the number of
  12. * breakpoints. See https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level
  13. */
  14. type BreakpointOrNull = Breakpoint | null
  15. export const useWidth = (): Breakpoint => {
  16. const theme: Theme = useTheme()
  17. const keys: readonly Breakpoint[] = [...theme.breakpoints.keys].reverse()
  18. return (
  19. keys.reduce((output: BreakpointOrNull, key: Breakpoint) => {
  20. // eslint-disable-next-line react-hooks/rules-of-hooks
  21. const matches = useMediaQuery(theme.breakpoints.up(key))
  22. return output != null && matches ? key : output
  23. }, null) ?? 'xs'
  24. )
  25. }

使用

  1. const breakpoint = useWidth()
展开查看全部
oknwwptz

oknwwptz3#

一个简单的钩子,将与默认断点xs,sm,md,lg和xl一起工作。此钩子适用于material ui版本5.11.x。您可以使用更多与断点相关的函数来扩展它。例如,当与props一起使用而不是使用props.size时,您可以使用props[getBreakPointName()].size作为响应大小值。
useBreakpoint.js钩子

  1. import { useMediaQuery, useTheme } from "@mui/material";
  2. /**
  3. * useBreakpoint
  4. *
  5. * @returns
  6. */
  7. const useBreakpoint = () => {
  8. /**
  9. * useTheme
  10. */
  11. const theme = useTheme();
  12. /**
  13. * useMediaQuery
  14. *
  15. */
  16. const mq_xs = useMediaQuery(theme.breakpoints.only('xs'));
  17. const mq_sm = useMediaQuery(theme.breakpoints.only('sm'));
  18. const mq_md = useMediaQuery(theme.breakpoints.only('md'));
  19. const mq_lg = useMediaQuery(theme.breakpoints.only('lg'));
  20. const mq_xl = useMediaQuery(theme.breakpoints.only('xl'));
  21. /**
  22. * getBreakPointName
  23. *
  24. */
  25. const getBreakPointName = () => {
  26. if(mq_xs){
  27. return "xs"
  28. }
  29. if(mq_sm){
  30. return "sm"
  31. }
  32. if(mq_md){
  33. return "md"
  34. }
  35. if(mq_lg){
  36. return "lg"
  37. }
  38. if(mq_xl){
  39. return "xl"
  40. }
  41. }
  42. return {
  43. getBreakPointName
  44. }
  45. }
  46. export default useBreakpoint
展开查看全部
bhmjp9jg

bhmjp9jg4#

对于em @Apoplectic的答案是不工作,所以我适应它。它仍然是一个定制的钩子。

  1. import {Theme, useTheme } from '@mui/material/styles' // or @mui/joy/styles
  2. import useMediaQuery from "@mui/material/useMediaQuery";
  3. import {Breakpoint} from "@mui/system";
  4. /**
  5. * taken from https://material-ui.com/components/use-media-query/#migrating-from-withwidth
  6. *
  7. * Be careful using this hook. It only works because the number of
  8. * breakpoints in theme is static. It will break once you change the number of
  9. * breakpoints. See https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level
  10. */
  11. type BreakpointOrNull = Breakpoint | null
  12. export const useWidth = (): Breakpoint => {
  13. const theme: Theme = useTheme()
  14. const keys: readonly Breakpoint[] = [...theme.breakpoints.keys]
  15. console.log(keys);
  16. return (
  17. keys.reduce((output: BreakpointOrNull, key: Breakpoint) => {
  18. // eslint-disable-next-line react-hooks/rules-of-hooks
  19. const matches = useMediaQuery(theme.breakpoints.up(key))
  20. return matches ? key : output
  21. }, null) ?? 'xs'
  22. )
  23. }

使用

  1. const breakpoint = useWidth()
展开查看全部
ffscu2ro

ffscu2ro5#

它的工作与此代码,如果不想使用typescript只是删除类型。

  1. const theme = useTheme();
  2. const keys: readonly Breakpoint[] = [...theme.breakpoints.keys].reverse();
  3. return (
  4. keys.filter((key: Breakpoint) => {
  5. // eslint-disable-next-line react-hooks/rules-of-hooks
  6. const matches = useMediaQuery(theme.breakpoints.up(key));
  7. if (matches) return true;
  8. return false;
  9. }, null)[0] ?? "xs"
  10. );

相关问题