-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
769ac8f
commit 6a2a9ff
Showing
20 changed files
with
700 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { ReactNode } from 'react'; | ||
|
||
export interface HasChildren { | ||
children: ReactNode; | ||
children?: ReactNode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useEffect } from 'react'; | ||
|
||
export function useElementHeightListener( | ||
ref: React.RefObject<HTMLDivElement>, | ||
listener: (height: number) => void | ||
) { | ||
useEffect(() => { | ||
if (!ref.current) return; | ||
|
||
const resizeObserver = new ResizeObserver(entries => { | ||
const observedHeight = entries[0].contentRect.height; | ||
listener(observedHeight); | ||
}); | ||
|
||
resizeObserver.observe(ref.current); | ||
|
||
return () => { | ||
// Cleanup the observer when the component is unmounted | ||
resizeObserver.disconnect(); | ||
}; | ||
}, [listener, ref]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useState } from 'react'; | ||
|
||
export function useRegisterChildren<T extends string>() { | ||
const [children, setChildren] = useState<Record<T, number>>({} as Record<T, number>); | ||
|
||
function registerChild(child: T) { | ||
setChildren(children => ({ ...children, [child]: (children[child] || 0) + 1 })); | ||
} | ||
|
||
function deregisterChild(child: T) { | ||
setChildren(children => ({ ...children, [child]: (children[child] || 0) - 1 })); | ||
} | ||
|
||
function hasChild(child: T) { | ||
return children[child] > 0; | ||
} | ||
|
||
return { children: Object.keys(children) as T[], registerChild, deregisterChild, hasChild }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Box, Circle, styled } from 'leather-styles/jsx'; | ||
|
||
import { Button } from '@app/ui/components/button/button'; | ||
import { Callout } from '@app/ui/components/callout/callout'; | ||
import { Flag } from '@app/ui/components/flag/flag'; | ||
import { ItemInteractive } from '@app/ui/components/item/item-interactive'; | ||
import { ItemLayout } from '@app/ui/components/item/item.layout'; | ||
|
||
import { Approver } from './approver'; | ||
|
||
export function ApproverDemo() { | ||
return ( | ||
<styled.div minH="100vh"> | ||
<Approver> | ||
<Approver.Header title="Some prompt that breaks two lines" requester="gamma.io" /> | ||
<Callout title="Some callout">Hey watch out for this sketchy app</Callout> | ||
<Approver.Section> | ||
<Approver.Subheader>Demo section 1</Approver.Subheader> | ||
<Flag img={<Circle size="40px" backgroundColor="ink.border-default" />} align="top"> | ||
<Box width="90%" height="16px" backgroundColor="ink.border-default" /> | ||
<Box width="75%" height="12px" backgroundColor="ink.border-default" mt="space.02" /> | ||
</Flag> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Demo section 2</Approver.Subheader> | ||
<ItemLayout | ||
titleLeft="Example" | ||
titleRight="Example" | ||
captionLeft="Example" | ||
captionRight="Example" | ||
flagImg={<Circle size="40px" backgroundColor="ink.border-default" />} | ||
/> | ||
</Approver.Section> | ||
<Approver.Advanced> | ||
<Approver.Section> | ||
Section 3 | ||
<ItemInteractive onClick={() => {}} mt="space.03"> | ||
<ItemLayout | ||
captionLeft="Example" | ||
captionRight="Example" | ||
flagImg={<Circle size="40px" backgroundColor="ink.border-default" />} | ||
titleLeft="Example" | ||
titleRight="Example" | ||
/> | ||
</ItemInteractive> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Demo section 1</Approver.Subheader> | ||
<Flag img={<Circle size="40px" backgroundColor="ink.border-default" />}> | ||
<Box width="100%" height="20px" backgroundColor="ink.border-default" /> | ||
</Flag> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Demo section 1</Approver.Subheader> | ||
<Flag img={<Circle size="40px" backgroundColor="ink.border-default" />}> | ||
<Box width="100%" height="20px" backgroundColor="ink.border-default" /> | ||
</Flag> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Demo section 1</Approver.Subheader> | ||
<Flag img={<Circle size="40px" backgroundColor="ink.border-default" />}> | ||
<Box width="100%" height="20px" backgroundColor="ink.border-default" /> | ||
</Flag> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Demo section 1</Approver.Subheader> | ||
<Flag img={<Circle size="40px" backgroundColor="ink.border-default" />}> | ||
<Box width="100%" height="20px" backgroundColor="ink.border-default" /> | ||
</Flag> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Demo section 1</Approver.Subheader> | ||
<Flag img={<Circle size="40px" backgroundColor="ink.border-default" />}> | ||
<Box width="100%" height="20px" backgroundColor="ink.border-default" /> | ||
</Flag> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Demo section 1</Approver.Subheader> | ||
<Flag img={<Circle size="40px" backgroundColor="ink.border-default" />}> | ||
<Box width="100%" height="20px" backgroundColor="ink.border-default" /> | ||
</Flag> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Demo section 1</Approver.Subheader> | ||
<Flag img={<Circle size="40px" backgroundColor="ink.border-default" />}> | ||
<Box width="100%" height="20px" backgroundColor="ink.border-default" /> | ||
</Flag> | ||
</Approver.Section> | ||
</Approver.Advanced> | ||
<Approver.Actions | ||
actions={ | ||
<> | ||
<Button variant="outline">Cancel</Button> | ||
<Button>Approve</Button> | ||
</> | ||
} | ||
/> | ||
</Approver> | ||
</styled.div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createContext, useContext } from 'react'; | ||
|
||
export type ApproverChildren = 'header' | 'actions' | 'advanced' | 'section' | 'subheader'; | ||
|
||
interface ApproverContext { | ||
isDisplayingAdvancedView: boolean; | ||
setIsDisplayingAdvancedView(val: boolean): void; | ||
registerChild(child: ApproverChildren): void; | ||
deregisterChild(child: ApproverChildren): void; | ||
hasChild(child: ApproverChildren): boolean; | ||
} | ||
|
||
const approverContext = createContext<ApproverContext | null>(null); | ||
|
||
export const ApproverProvider = approverContext.Provider; | ||
|
||
export function useApproverContext() { | ||
const context = useContext(approverContext); | ||
if (!context) throw new Error('`useApproverContext` must be used within a `ApproverProvider`'); | ||
return context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import { Box, Circle, Flex } from 'leather-styles/jsx'; | ||
|
||
import { Button } from '@app/ui/components/button/button'; | ||
import { Callout } from '@app/ui/components/callout/callout'; | ||
import { Flag } from '@app/ui/components/flag/flag'; | ||
import { ItemInteractive } from '@app/ui/components/item/item-interactive'; | ||
import { ItemLayout } from '@app/ui/components/item/item.layout'; | ||
|
||
import { Approver } from './approver'; | ||
|
||
const meta: Meta<typeof Approver> = { | ||
component: Approver, | ||
tags: ['autodocs'], | ||
title: 'Feature/Approver', | ||
|
||
render: ({ children, ...args }) => ( | ||
<Flex maxW="390px" h="680px" border="1px solid lightgrey" overflowY="auto"> | ||
<Approver {...args}>{children}</Approver> | ||
</Flex> | ||
), | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof Approver>; | ||
|
||
export const ExampleOne: Story = { | ||
args: { | ||
children: ( | ||
<> | ||
<Approver.Header title="Some prompt that breaks two lines" requester="gamma.io" /> | ||
<Callout title="Some callout">Hey watch out for this sketchy app</Callout> | ||
<Approver.Section> | ||
<Approver.Subheader>Example flag content</Approver.Subheader> | ||
<Flag img={<Circle size="40px" backgroundColor="ink.border-default" />} align="top"> | ||
<Box width="90%" height="16px" backgroundColor="ink.border-default" /> | ||
<Box width="75%" height="12px" backgroundColor="ink.border-default" mt="space.02" /> | ||
</Flag> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Example rich content with avatar</Approver.Subheader> | ||
<ItemLayout | ||
titleLeft="Michelly token" | ||
titleRight="100 MICA" | ||
captionLeft="SIP-10" | ||
captionRight="$894,891" | ||
flagImg={<Circle size="40px" backgroundColor="ink.border-default" />} | ||
/> | ||
</Approver.Section> | ||
<Approver.Advanced> | ||
<Approver.Section> | ||
<Approver.Subheader>In the advanced section</Approver.Subheader> | ||
<ItemInteractive onClick={() => {}} mt="space.03" mb="space.03"> | ||
<ItemLayout | ||
titleLeft="Pressable" | ||
titleRight="Mr. Clicky" | ||
captionLeft="Interactive item" | ||
captionRight="Click me" | ||
flagImg={<Circle size="40px" backgroundColor="ink.border-default" />} | ||
/> | ||
</ItemInteractive> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Inputs & Outputs</Approver.Subheader> | ||
<Flag align="top" img={<Circle size="40px" backgroundColor="ink.border-default" />}> | ||
<Box width="100%" height="20px" backgroundColor="ink.border-default" /> | ||
</Flag> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Transaction raw data</Approver.Subheader> | ||
<Flag align="top" img={<Circle size="40px" backgroundColor="ink.border-default" />}> | ||
<Box width="100%" height="20px" backgroundColor="ink.border-default" /> | ||
</Flag> | ||
</Approver.Section> | ||
<Approver.Section> | ||
<Approver.Subheader>Additional info</Approver.Subheader> | ||
<Flag align="top" img={<Circle size="40px" backgroundColor="ink.border-default" />}> | ||
<Box width="100%" height="20px" backgroundColor="ink.border-default" /> | ||
</Flag> | ||
</Approver.Section> | ||
</Approver.Advanced> | ||
|
||
<Approver.Actions | ||
actions={ | ||
<> | ||
<Button variant="outline">Cancel</Button> | ||
<Button>Approve</Button> | ||
</> | ||
} | ||
/> | ||
</> | ||
), | ||
}, | ||
}; | ||
|
||
export const ActionsAlignToBottom: Story = { | ||
args: { | ||
children: ( | ||
<> | ||
<Approver.Header | ||
title="Action align to bottom of page" | ||
requester="even when there's no content to push it there" | ||
/> | ||
<Approver.Actions | ||
actions={ | ||
<> | ||
<Button variant="outline">Cancel</Button> | ||
<Button>Approve</Button> | ||
</> | ||
} | ||
/> | ||
</> | ||
), | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useState } from 'react'; | ||
|
||
import { css } from 'leather-styles/css'; | ||
import { Flex, styled } from 'leather-styles/jsx'; | ||
|
||
import type { HasChildren } from '@app/common/has-children'; | ||
import { useRegisterChildren } from '@app/common/hooks/use-register-children'; | ||
|
||
import { type ApproverChildren, ApproverProvider } from './approver.context'; | ||
import { ApproverActions } from './components/approver-actions'; | ||
import { ApproverAdvanced } from './components/approver-advanced'; | ||
import { ApproverHeader } from './components/approver-header'; | ||
import { ApproverSection } from './components/approver-section'; | ||
import { ApproverSubheader } from './components/approver-subheader'; | ||
|
||
const applyMarginsToLastApproverSection = css({ | ||
'& .approver-section:last-child': { mb: 'space.03' }, | ||
}); | ||
|
||
function Approver({ children }: HasChildren) { | ||
const { registerChild, deregisterChild, hasChild } = useRegisterChildren<ApproverChildren>(); | ||
const [isDisplayingAdvancedView, setIsDisplayingAdvancedView] = useState(false); | ||
|
||
return ( | ||
<ApproverProvider | ||
value={{ | ||
hasChild, | ||
registerChild, | ||
deregisterChild, | ||
isDisplayingAdvancedView, | ||
setIsDisplayingAdvancedView, | ||
}} | ||
> | ||
<styled.main | ||
display="flex" | ||
flexDir="column" | ||
pos="relative" | ||
minH="100%" | ||
maxW="640px" | ||
mx="auto" | ||
className={applyMarginsToLastApproverSection} | ||
alignItems="center" | ||
> | ||
<Flex flexDir="column" flex={1} background="ink.background-secondary"> | ||
{children} | ||
</Flex> | ||
</styled.main> | ||
</ApproverProvider> | ||
); | ||
} | ||
|
||
Approver.Header = ApproverHeader; | ||
Approver.Subheader = ApproverSubheader; | ||
Approver.Section = ApproverSection; | ||
Approver.Advanced = ApproverAdvanced; | ||
Approver.Actions = ApproverActions; | ||
|
||
export { Approver }; |
Oops, something went wrong.