forked from morfeojs/morfeo
-
Notifications
You must be signed in to change notification settings - Fork 0
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
8416a84
commit 746228a
Showing
8 changed files
with
215 additions
and
50 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
devtool/src/_shared/components/Slices/Borders/BorderCard/BorderCard.tsx
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,83 @@ | ||
import { useMemo } from 'react'; | ||
import { useStyle, useThemeValue, Border, Color, Size } from '@morfeo/react'; | ||
import { getContrast } from 'polished'; | ||
import { Card } from '../../../Card'; | ||
import { Props as DetailProps } from '../Detail' | ||
|
||
export type Props = { | ||
clickable?: boolean; | ||
detailKey: string; | ||
showValues?: boolean; | ||
mainSlice: DetailProps['mainSlice']; | ||
filters?: Record<'borderWidths' | 'borderStyles' | 'colors', string> | ||
}; | ||
|
||
export const BorderCard: React.FC<Props> = ({ | ||
clickable, | ||
detailKey, | ||
showValues, | ||
mainSlice, | ||
filters, | ||
}) => { | ||
const isBorders = mainSlice === 'borders'; | ||
const themeValue = useThemeValue(mainSlice, detailKey as any); | ||
const borderColor = useThemeValue('colors', (isBorders ? themeValue.color : filters?.colors || '') as Color); | ||
const style = useStyle({ | ||
border: detailKey as Border, | ||
}); | ||
|
||
const contrastRatio = useMemo(() => { | ||
if (!borderColor || borderColor === 'none') { | ||
return 2; | ||
} | ||
return getContrast(borderColor || '#000000' as string, '#ffffff'); | ||
}, [borderColor]); | ||
|
||
const innerCardStyle = useMemo(() => { | ||
if (mainSlice === 'borders') { | ||
return { | ||
border: detailKey | ||
} | ||
} | ||
if (mainSlice === 'borderWidths') { | ||
return { | ||
borderWidth: detailKey, | ||
borderColor: filters?.colors || borderColor || 'var(--colors-dark)', | ||
borderStyle: filters?.borderStyles || 'var(--border-styles-solid)' | ||
} | ||
} | ||
if (mainSlice === 'borderStyles') { | ||
return { | ||
borderStyle: detailKey, | ||
borderColor: filters?.colors || borderColor || 'var(--colors-dark)', | ||
borderWidth: filters?.borderWidths || 'var(--border-widths-m)' | ||
} | ||
} | ||
return {} | ||
}, [borderColor, detailKey, filters?.borderStyles, filters?.borderWidths, filters?.colors, mainSlice]) | ||
|
||
return ( | ||
<> | ||
<Card | ||
className={`${ | ||
clickable ? 'morfeo-card-primary-clickable' : 'morfeo-card-primary' | ||
} ${contrastRatio < 1.95 ? 'bg-black' : 'bg-white'}`} | ||
> | ||
<Card | ||
style={ | ||
{ | ||
...innerCardStyle, | ||
size: '100%', | ||
} as any | ||
} | ||
/> | ||
</Card> | ||
{showValues && ( | ||
<h5 className="morfeo-typography-h5"> | ||
<span className="font-weight-bold">Value: </span> | ||
{style.border} | ||
</h5> | ||
)} | ||
</> | ||
); | ||
}; |
88 changes: 69 additions & 19 deletions
88
devtool/src/_shared/components/Slices/Borders/Detail/index.tsx
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,30 +1,80 @@ | ||
import React from 'react'; | ||
import { Border, morfeo } from '@morfeo/react'; | ||
import { Card } from '../../../Card'; | ||
import React, { useMemo, useState, useCallback } from 'react'; | ||
import { useRouter } from '../../../../hooks'; | ||
import { RouteState } from '../../../../contexts'; | ||
import { BorderCard } from '../BorderCard/BorderCard'; | ||
import styles from './style.module.css'; | ||
import { BorderSlice } from '../index'; | ||
import { capitalCase, noCase } from 'change-case'; | ||
import { morfeo } from '@morfeo/react'; | ||
import { DropDown } from '../../../DropDown'; | ||
import { Grid, Item } from '../../../Grid'; | ||
|
||
export type Props = { | ||
mainSlice: BorderSlice; | ||
}; | ||
|
||
const dropDownsMap = { | ||
borders: [], | ||
borderStyles: ['borderWidths', 'colors'] as const, | ||
borderWidths: ['borderStyles', 'colors'] as const, | ||
}; | ||
|
||
export const Detail: React.FC<Props> = ({ mainSlice }) => { | ||
const [filters, setFilters] = useState({ | ||
borderStyles: '', | ||
borderWidths: '', | ||
colors: '', | ||
}); | ||
|
||
export const Detail: React.FC = () => { | ||
const { route } = useRouter(); | ||
const { state = {} as RouteState } = route; | ||
const { detailKey } = state; | ||
const value = morfeo.resolve({ border: detailKey as Border })['border']; | ||
|
||
const onChangeFilter = useCallback( | ||
(slice: BorderSlice | 'colors', value: string) => { | ||
setFilters(state => ({ | ||
...state, | ||
[slice]: value, | ||
})); | ||
}, | ||
[], | ||
); | ||
|
||
const dropdowns = useMemo(() => { | ||
return dropDownsMap[mainSlice].map(slice => { | ||
const title = capitalCase(noCase(slice)); | ||
const values = morfeo.getTheme()[slice]; | ||
const options = Object.keys(values || {}).map(option => ({ | ||
label: capitalCase(noCase(option)), | ||
value: option, | ||
})); | ||
|
||
return ( | ||
<Item key={slice}> | ||
<DropDown | ||
value={filters[slice]} | ||
title={title} | ||
options={options} | ||
onChange={value => onChangeFilter(slice, value)} | ||
inverted | ||
placeholder={slice} | ||
/> | ||
</Item> | ||
); | ||
}); | ||
}, [filters, mainSlice, onChangeFilter]); | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<Card | ||
className="morfeo-card-primary" | ||
copyText={detailKey} | ||
style={ | ||
{ | ||
border: detailKey as Border, | ||
size: '200px', | ||
} as any | ||
} | ||
> | ||
<h2 className="morfeo-typography-h2">{value}</h2> | ||
</Card> | ||
</div> | ||
<> | ||
<Grid>{dropdowns}</Grid> | ||
<div className={styles.container}> | ||
<BorderCard | ||
filters={filters} | ||
mainSlice={mainSlice} | ||
detailKey={detailKey as string} | ||
showValues | ||
/> | ||
</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
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
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