-
Notifications
You must be signed in to change notification settings - Fork 9
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
31 changed files
with
889 additions
and
87 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
self.addEventListener('install', () => { | ||
// TODO: Do something. | ||
}); |
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,88 @@ | ||
import { | ||
datatypes, | ||
JSONValue, | ||
properties, | ||
Resource, | ||
useResource, | ||
useSubject, | ||
useTitle, | ||
} from '@tomic/react'; | ||
import React, { useMemo } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
export interface AllPropsSimpleProps { | ||
resource: Resource; | ||
} | ||
|
||
/** Renders a simple list of all properties on the resource. Will not render any link or other interactive element. */ | ||
export function AllPropsSimple({ resource }: AllPropsSimpleProps): JSX.Element { | ||
return ( | ||
<ul> | ||
{[...resource.getPropVals()].map(([prop, val]) => ( | ||
<Row key={prop} prop={prop} val={val} /> | ||
))} | ||
</ul> | ||
); | ||
} | ||
|
||
interface RowProps { | ||
prop: string; | ||
val: JSONValue; | ||
} | ||
|
||
function Row({ prop, val }: RowProps): JSX.Element { | ||
const propResource = useResource(prop); | ||
const [propName] = useTitle(propResource); | ||
const [dataType] = useSubject(propResource, properties.datatype); | ||
|
||
const value = useMemo(() => { | ||
if (dataType === datatypes.atomicUrl) { | ||
return <Value val={val as string} />; | ||
} | ||
|
||
if (dataType === datatypes.resourceArray) { | ||
return <ResourceArray val={val as string[]} />; | ||
} | ||
|
||
return <>{val as string}</>; | ||
}, [val, dataType]); | ||
|
||
return ( | ||
<List> | ||
<Key>{propName}</Key>: {value} | ||
</List> | ||
); | ||
} | ||
|
||
const Key = styled.span` | ||
font-weight: bold; | ||
`; | ||
|
||
const List = styled.ul` | ||
list-style: none; | ||
margin: 0; | ||
white-space: nowrap; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
color: ${p => p.theme.colors.textLight}; | ||
`; | ||
|
||
function ResourceArray({ val }: { val: string[] }): JSX.Element { | ||
return ( | ||
<> | ||
{val.map((v, i) => ( | ||
<> | ||
<Value val={v} key={v} /> | ||
{i === val.length - 1 ? '' : ', '} | ||
</> | ||
))} | ||
</> | ||
); | ||
} | ||
|
||
function Value({ val }: { val: string }): JSX.Element { | ||
const valueResource = useResource(val); | ||
const [valueName] = useTitle(valueResource); | ||
|
||
return <>{valueName}</>; | ||
} |
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,129 @@ | ||
import React, { useCallback, useId, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
export interface ButtonGroupOption { | ||
label: string; | ||
icon: React.ReactNode; | ||
value: string; | ||
checked?: boolean; | ||
} | ||
|
||
export interface ButtonGroupProps { | ||
options: ButtonGroupOption[]; | ||
name: string; | ||
onChange: (value: string) => void; | ||
} | ||
|
||
export function ButtonGroup({ | ||
options, | ||
name, | ||
onChange, | ||
}: ButtonGroupProps): JSX.Element { | ||
const [selected, setSelected] = useState( | ||
() => options.find(o => o.checked)?.value, | ||
); | ||
|
||
const handleChange = useCallback( | ||
(checked: boolean, value: string) => { | ||
if (checked) { | ||
onChange(value); | ||
setSelected(value); | ||
} | ||
}, | ||
[onChange], | ||
); | ||
|
||
return ( | ||
<Group> | ||
{options.map(option => ( | ||
<ButtonGroupItem | ||
{...option} | ||
key={option.value} | ||
onChange={handleChange} | ||
checked={selected === option.value} | ||
name={name} | ||
/> | ||
))} | ||
</Group> | ||
); | ||
} | ||
|
||
interface ButtonGroupItemProps extends ButtonGroupOption { | ||
onChange: (checked: boolean, value: string) => void; | ||
name: string; | ||
} | ||
|
||
function ButtonGroupItem({ | ||
onChange, | ||
icon, | ||
label, | ||
name, | ||
value, | ||
checked, | ||
}: ButtonGroupItemProps): JSX.Element { | ||
const id = useId(); | ||
|
||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
onChange(event.target.checked, value); | ||
}; | ||
|
||
return ( | ||
<Item> | ||
<Input | ||
id={id} | ||
type='radio' | ||
onChange={handleChange} | ||
name={name} | ||
value={value} | ||
checked={checked} | ||
/> | ||
<Label htmlFor={id} title={label}> | ||
{icon} | ||
</Label> | ||
</Item> | ||
); | ||
} | ||
|
||
const Group = styled.form` | ||
display: flex; | ||
height: 2rem; | ||
gap: 0.5rem; | ||
`; | ||
|
||
const Item = styled.div` | ||
position: relative; | ||
width: 2rem; | ||
aspect-ratio: 1/1; | ||
`; | ||
|
||
const Label = styled.label` | ||
position: absolute; | ||
inset: 0; | ||
width: 100%; | ||
aspect-ratio: 1/1; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
border-radius: ${p => p.theme.radius}; | ||
color: ${p => p.theme.colors.textLight}; | ||
cursor: pointer; | ||
transition: background-color 0.1s ease-in-out, color 0.1s ease-in-out; | ||
input:checked + & { | ||
background-color: ${p => p.theme.colors.bg1}; | ||
color: ${p => p.theme.colors.text}; | ||
} | ||
:hover { | ||
background-color: ${p => p.theme.colors.bg1}; | ||
} | ||
`; | ||
|
||
const Input = styled.input` | ||
position: absolute; | ||
inset: 0; | ||
width: 100%; | ||
aspect-ratio: 1/1; | ||
visibility: hidden; | ||
`; |
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 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
Oops, something went wrong.