From f9a6e8667179186ee923ffb64d7d20967009b164 Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Sun, 7 Jan 2024 11:09:26 +0200 Subject: [PATCH] feat: scroll to last announcement, add clear button --- .storybook/Example.stories.tsx | 34 ++++++++++++++- src/Panel.tsx | 76 ++++++++++++++++++++++++++++------ 2 files changed, 97 insertions(+), 13 deletions(-) diff --git a/.storybook/Example.stories.tsx b/.storybook/Example.stories.tsx index 6e471c7..dc415c6 100644 --- a/.storybook/Example.stories.tsx +++ b/.storybook/Example.stories.tsx @@ -1,4 +1,10 @@ -import React, { useLayoutEffect, useMemo, useReducer, useRef } from 'react'; +import React, { + useLayoutEffect, + useMemo, + useReducer, + useRef, + useState, +} from 'react'; export default { title: 'Example', @@ -65,6 +71,32 @@ export function ShadowDOM() { ); } +export function Intervals() { + const [isVisible, toggleVisible] = useReducer((s) => !s, false); + const [interval, setIntervalState] = useState | null>(); + + function onClick() { + if (interval != null) { + clearInterval(interval); + setIntervalState(null); + return; + } + + setIntervalState(setInterval(toggleVisible, 500)); + } + + return ( + <> + + +
{isVisible && 'Notification'}
+
{isVisible && 'Alert'}
+ + ); +} + function Toggle({ children, }: { diff --git a/src/Panel.tsx b/src/Panel.tsx index ad5d689..831f077 100644 --- a/src/Panel.tsx +++ b/src/Panel.tsx @@ -1,5 +1,5 @@ -import React from 'react'; -import { useAddonState, useChannel } from "@storybook/manager-api"; +import React, { useRef } from 'react'; +import { useAddonState, useChannel } from '@storybook/manager-api'; import { AddonPanel } from '@storybook/components'; import { STORY_CHANGED } from '@storybook/core-events'; import { styled } from '@storybook/theming'; @@ -12,6 +12,12 @@ interface Announcement { politenessSetting: PolitenessSetting; } +const Wrapper = styled.div` + position: relative; + max-height: 100%; + overflow: auto; +`; + const List = styled.ol` list-style: none; margin: 0; @@ -23,6 +29,20 @@ const ListItem = styled.li` margin: 0.5rem 0; `; +const Button = styled.button` + margin: 0; + padding: 0; + background: transparent; + border: 0; + height: 2rem; + width: 2rem; + cursor: pointer; + position: absolute; + top: 0.5rem; + right: 1rem; + z-index: 2; +`; + const PolitenessLabel = styled.span` font-weight: 600; text-transform: uppercase; @@ -46,16 +66,48 @@ export const Panel: React.FC<{ active: boolean }> = (props) => { return ( - - {captures.map((capture, index) => ( - - - [{capture.politenessSetting}] - {' '} - {capture.textContent} - - ))} - + + + + + {captures.map((capture, index) => ( + { + // Automatically scroll to last item + if (ref && index === captures.length - 1) { + ref.scrollIntoView(); + } + }} + > + + [{capture.politenessSetting}] + {' '} + {capture.textContent} + + ))} + + ); }; + +// https://akveo.github.io/eva-icons/#/?searchKey=refresh&type=outline +function RestartIcon() { + return ( + + + + + + + + + ); +}