diff --git a/jest.config.js b/jest.config.js deleted file mode 100644 index 0c6601ae..00000000 --- a/jest.config.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - setupFiles: ['./tests/setup.js'], - snapshotSerializers: [require.resolve('enzyme-to-json/serializer')], -}; \ No newline at end of file diff --git a/src/ScrollBar.tsx b/src/ScrollBar.tsx index 832eadd1..c57ad969 100644 --- a/src/ScrollBar.tsx +++ b/src/ScrollBar.tsx @@ -45,7 +45,6 @@ const ScrollBar = React.forwardRef((props, ref) => const [dragging, setDragging] = React.useState(false); const [pageXY, setPageXY] = React.useState(null); const [startTop, setStartTop] = React.useState(null); - const [dark, setDark] = React.useState(false); const isLTR = !rtl; @@ -188,21 +187,6 @@ const ScrollBar = React.forwardRef((props, ref) => } }, [dragging]); - React.useEffect(() => { - const media = window.matchMedia?.('(prefers-color-scheme: dark)'); - setDark(media.matches); - - const listener = (e: MediaQueryListEvent) => { - setDark(e.matches); - }; - - media?.addEventListener('change', listener); - - return () => { - media?.removeEventListener('change', listener); - }; - }, []); - React.useEffect(() => { delayHidden(); return () => { @@ -225,45 +209,38 @@ const ScrollBar = React.forwardRef((props, ref) => const thumbStyle: React.CSSProperties = { position: 'absolute', - background: dark ? 'rgba(255, 255, 255, 0.5)' : 'rgba(0, 0, 0, 0.5)', borderRadius: 99, + background: 'var(--rc-virtual-list-scrollbar-bg, rgba(0, 0, 0, 0.5))', cursor: 'pointer', userSelect: 'none', - ...propsThumbStyle, }; if (horizontal) { - // Container - containerStyle.height = 8; - containerStyle.left = 0; - containerStyle.right = 0; - containerStyle.bottom = 0; - - // Thumb - thumbStyle.height = '100%'; - thumbStyle.width = spinSize; - - if (isLTR) { - thumbStyle.left = top; - } else { - thumbStyle.right = top; - } + Object.assign(containerStyle, { + height: 8, + left: 0, + right: 0, + bottom: 0, + }); + + Object.assign(thumbStyle, { + height: '100%', + width: spinSize, + [isLTR ? 'left' : 'right']: top, + }); } else { - // Container - containerStyle.width = 8; - containerStyle.top = 0; - containerStyle.bottom = 0; - - if (isLTR) { - containerStyle.right = 0; - } else { - containerStyle.left = 0; - } - - // Thumb - thumbStyle.width = '100%'; - thumbStyle.height = spinSize; - thumbStyle.top = top; + Object.assign(containerStyle, { + width: 8, + top: 0, + bottom: 0, + [isLTR ? 'right' : 'left']: 0, + }); + + Object.assign(thumbStyle, { + width: '100%', + height: spinSize, + top, + }); } return ( @@ -283,7 +260,7 @@ const ScrollBar = React.forwardRef((props, ref) => className={classNames(`${scrollbarPrefixCls}-thumb`, { [`${scrollbarPrefixCls}-thumb-moving`]: dragging, })} - style={{ ...thumbStyle }} + style={{ ...thumbStyle, ...propsThumbStyle }} onMouseDown={onThumbMouseDown} /> diff --git a/tests/dark.test.js b/tests/dark.test.js deleted file mode 100644 index 6e8454ae..00000000 --- a/tests/dark.test.js +++ /dev/null @@ -1,101 +0,0 @@ -import React from 'react'; -import { mount } from 'enzyme'; -import { act } from 'react-dom/test-utils'; -import List from '../src'; - -describe('List.dark', () => { - const originalMatchMedia = window.matchMedia; - - afterEach(() => { - window.matchMedia = originalMatchMedia; - }); - - const mockMatchMedia = (matches) => { - Object.defineProperty(window, 'matchMedia', { - writable: true, - value: jest.fn().mockImplementation(query => ({ - matches, - media: query, - onchange: null, - addListener: jest.fn(), // deprecated - removeListener: jest.fn(), // deprecated - addEventListener: jest.fn(), - removeEventListener: jest.fn(), - dispatchEvent: jest.fn(), - })), - }); - }; - - it('should render dark scrollbar', () => { - mockMatchMedia(true); - - const wrapper = mount( - item}> - {(item) =>
{item}
} -
- ); - - const thumb = wrapper.find('.rc-virtual-list-scrollbar-thumb'); - expect(thumb.props().style.background).toBe('rgba(255, 255, 255, 0.5)'); - }); - - it('should render light scrollbar', () => { - mockMatchMedia(false); - - const wrapper = mount( - item}> - {(item) =>
{item}
} -
- ); - - const thumb = wrapper.find('.rc-virtual-list-scrollbar-thumb'); - expect(thumb.props().style.background).toBe('rgba(0, 0, 0, 0.5)'); - }); - - it('should update on theme change', () => { - let listener; - Object.defineProperty(window, 'matchMedia', { - writable: true, - value: jest.fn().mockImplementation(() => ({ - matches: false, - addEventListener: (type, cb) => { - if (type === 'change') { - listener = cb; - } - }, - removeEventListener: () => {}, - })), - }); - - const wrapper = mount( - item}> - {(item) =>
{item}
} -
- ); - - // Initial: light - expect(wrapper.find('.rc-virtual-list-scrollbar-thumb').props().style.background).toBe( - 'rgba(0, 0, 0, 0.5)', - ); - - // Change to dark - act(() => { - listener({ matches: true }); - }); - wrapper.update(); - - expect(wrapper.find('.rc-virtual-list-scrollbar-thumb').props().style.background).toBe( - 'rgba(255, 255, 255, 0.5)', - ); - - // Change back to light - act(() => { - listener({ matches: false }); - }); - wrapper.update(); - - expect(wrapper.find('.rc-virtual-list-scrollbar-thumb').props().style.background).toBe( - 'rgba(0, 0, 0, 0.5)', - ); - }); -}); \ No newline at end of file diff --git a/tests/setup.js b/tests/setup.js deleted file mode 100644 index 87237082..00000000 --- a/tests/setup.js +++ /dev/null @@ -1,13 +0,0 @@ -Object.defineProperty(window, 'matchMedia', { - writable: true, - value: jest.fn().mockImplementation(query => ({ - matches: false, - media: query, - onchange: null, - addListener: jest.fn(), // deprecated - removeListener: jest.fn(), // deprecated - addEventListener: jest.fn(), - removeEventListener: jest.fn(), - dispatchEvent: jest.fn(), - })), -}); \ No newline at end of file