react-sticky-box icon indicating copy to clipboard operation
react-sticky-box copied to clipboard

Responsive - deactivate stickybox on mobile devices

Open adrienlamotte opened this issue 5 years ago • 8 comments

Hello,

Most of the time when using a sticky sidebar, you want the sidebar not to be sticky on small devices. Is there any way to achieve this ?

Thank you for your work, great lib btw :)

adrienlamotte avatar Jan 31 '20 09:01 adrienlamotte

did you find a solution to it ? I have same challenge

johnsonthedev avatar Jun 03 '20 08:06 johnsonthedev

@Johnson444 well for now I define a conditionnal component like :

const StickyWrapper = isLargeScreen ? StickyBox : React.Fragment;

Where isLargeScreen is your test to check the breakpoint.

adrienlamotte avatar Jun 03 '20 08:06 adrienlamotte

@adrienlamotte Thx ! Will do this as well. If I have time I will look closer into the package. If I find a better way I will let you know

johnsonthedev avatar Jun 03 '20 08:06 johnsonthedev

@Johnson444 well for now I define a conditionnal component like :

const StickyWrapper = isLargeScreen ? StickyBox : React.Fragment;

Where isLargeScreen is your test to check the breakpoint.

This solution might work, when the children can be re-rendered. In case that the children should not be re-rendered this might kill some flow.

I would prefer like a prop disable , that just disable the calculcation from StickyBox and act just an simple div

          <StickyBox
            offsetTop={160}
            offsetBottom={0}
            disable={!isLargeScreen}
          >

endze1t avatar Sep 20 '20 13:09 endze1t

A simple and fast solution to remove stickyBox on mobile is to add a css class and use @media to cancel the sticky effect according to the desired width. Here is an example :

// In component
<StickyBox className="stickyBox">
    <YourComponent />
</StickyBox>

/*CSS*/
@media (max-width: 1024px) { 
  .stickyBox{
    position: inherit !important;
    top: inherit !important;
  }
}

I hope it helps ++.

Arnaud-Desportes avatar Mar 28 '21 11:03 Arnaud-Desportes

@Arnaud-Desportes Nice solution, thanks for sharing!

simplenotezy avatar Nov 21 '21 22:11 simplenotezy

@Arnaud-Desportes

You saved me lots of hours, Thanks for the solution!

namipsg avatar Jun 27 '22 09:06 namipsg

MUI 5 solution thanks to @Arnaud-Desportes

import React from 'react';
import { SxProps, Theme, styled } from '@mui/material/styles';

import ReactStickyBox, { StickyBoxCompProps } from 'react-sticky-box';

export interface StickyBoxProps extends StickyBoxCompProps {
  disabled?: boolean;
  sx?: SxProps<Theme>;
}

const StickyBox = styled(ReactStickyBox, {
  shouldForwardProp: (prop) => ['disabled'].every((key) => key !== prop),
})<StickyBoxProps>(({ theme, disabled }) =>
  theme.unstable_sx({
    ...(disabled && {
      position: 'inherit !important',
      top: 'inherit !important',
    }),
  }),
);

export default StickyBox;

antokhio avatar Jun 06 '24 09:06 antokhio