-
-
Notifications
You must be signed in to change notification settings - Fork 270
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
Showing
8 changed files
with
524 additions
and
163 deletions.
There are no files selected for viewing
409 changes: 355 additions & 54 deletions
409
src/app/components/image-pack-view/ImagePackContent.tsx
Large diffs are not rendered by default.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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,115 @@ | ||
import React, { MouseEventHandler, useMemo, useState } from 'react'; | ||
import { Box, Button, config, Icon, Icons, Menu, MenuItem, PopOut, RectCords, Text } from 'folds'; | ||
import FocusTrap from 'focus-trap-react'; | ||
import { ImageUsage } from '../../plugins/custom-emoji'; | ||
import { stopPropagation } from '../../utils/keyboard'; | ||
|
||
export const useUsageStr = (): ((usage: ImageUsage[]) => string) => { | ||
const getUsageStr = (usage: ImageUsage[]): string => { | ||
const sticker = usage.includes(ImageUsage.Sticker); | ||
const emoticon = usage.includes(ImageUsage.Emoticon); | ||
|
||
if (sticker && emoticon) return 'Emoji & Sticker'; | ||
if (sticker) return 'Sticker'; | ||
if (emoticon) return 'Emoji'; | ||
return 'Both'; | ||
}; | ||
return getUsageStr; | ||
}; | ||
|
||
type UsageSelectorProps = { | ||
selected: ImageUsage[]; | ||
onChange: (usage: ImageUsage[]) => void; | ||
}; | ||
export function UsageSelector({ selected, onChange }: UsageSelectorProps) { | ||
const getUsageStr = useUsageStr(); | ||
|
||
const selectedUsageStr = getUsageStr(selected); | ||
const isSelected = (usage: ImageUsage[]) => getUsageStr(usage) === selectedUsageStr; | ||
|
||
const allUsages: ImageUsage[][] = useMemo( | ||
() => [[ImageUsage.Emoticon], [ImageUsage.Sticker], [ImageUsage.Sticker, ImageUsage.Emoticon]], | ||
[] | ||
); | ||
|
||
return ( | ||
<Box direction="Column" gap="100" style={{ padding: config.space.S100 }}> | ||
{allUsages.map((usage) => ( | ||
<MenuItem | ||
key={getUsageStr(usage)} | ||
size="300" | ||
variant={isSelected(usage) ? 'SurfaceVariant' : 'Surface'} | ||
aria-selected={isSelected(usage)} | ||
radii="300" | ||
onClick={() => onChange(usage)} | ||
> | ||
<Box grow="Yes"> | ||
<Text size="T300">{getUsageStr(usage)}</Text> | ||
</Box> | ||
</MenuItem> | ||
))} | ||
</Box> | ||
); | ||
} | ||
|
||
type UsageSwitcherProps = { | ||
usage: ImageUsage[]; | ||
canEdit?: boolean; | ||
onChange: (usage: ImageUsage[]) => void; | ||
}; | ||
export function UsageSwitcher({ usage, onChange, canEdit }: UsageSwitcherProps) { | ||
const getUsageStr = useUsageStr(); | ||
|
||
const [menuCords, setMenuCords] = useState<RectCords>(); | ||
|
||
const handleSelectUsage: MouseEventHandler<HTMLButtonElement> = (event) => { | ||
setMenuCords(event.currentTarget.getBoundingClientRect()); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button | ||
variant="Secondary" | ||
fill="Soft" | ||
size="300" | ||
radii="300" | ||
outlined | ||
aria-disabled={!canEdit} | ||
after={canEdit && <Icon src={Icons.ChevronBottom} size="100" />} | ||
onClick={canEdit ? handleSelectUsage : undefined} | ||
> | ||
<Text size="B300">{getUsageStr(usage)}</Text> | ||
</Button> | ||
<PopOut | ||
anchor={menuCords} | ||
offset={5} | ||
position="Bottom" | ||
align="End" | ||
content={ | ||
<FocusTrap | ||
focusTrapOptions={{ | ||
initialFocus: false, | ||
onDeactivate: () => setMenuCords(undefined), | ||
clickOutsideDeactivates: true, | ||
isKeyForward: (evt: KeyboardEvent) => | ||
evt.key === 'ArrowDown' || evt.key === 'ArrowRight', | ||
isKeyBackward: (evt: KeyboardEvent) => | ||
evt.key === 'ArrowUp' || evt.key === 'ArrowLeft', | ||
escapeDeactivates: stopPropagation, | ||
}} | ||
> | ||
<Menu> | ||
<UsageSelector | ||
selected={usage} | ||
onChange={(usg) => { | ||
setMenuCords(undefined); | ||
onChange(usg); | ||
}} | ||
/> | ||
</Menu> | ||
</FocusTrap> | ||
} | ||
/> | ||
</> | ||
); | ||
} |
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 |
---|---|---|
|
@@ -38,4 +38,8 @@ export class PackMetaReader { | |
|
||
return knownUsage; | ||
} | ||
|
||
get content(): PackMeta { | ||
return this.meta; | ||
} | ||
} |
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