-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Builder): move components to single files, create hooks and …
…create state files (#16279) * refactor(Builder): move components to single files and create hooks * refactor(Builder): add aria label
- Loading branch information
1 parent
2d86556
commit be53c0f
Showing
11 changed files
with
520 additions
and
487 deletions.
There are no files selected for viewing
401 changes: 10 additions & 391 deletions
401
packages/fluentui/react-builder/src/components/Designer.tsx
Large diffs are not rendered by default.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
packages/fluentui/react-builder/src/components/ErrorPanel.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,37 @@ | ||
import * as React from 'react'; | ||
import { ErrorIcon } from '@fluentui/react-icons-northstar'; | ||
import { Text, Accordion } from '@fluentui/react-northstar'; | ||
|
||
export const ErrorPanel = ({ axeErrors }) => { | ||
const panels = [ | ||
{ | ||
key: 'axe', | ||
title: { | ||
'aria-level': 4, | ||
content: ( | ||
<Text> | ||
<ErrorIcon style={{ marginRight: '0.5rem' }} /> {axeErrors.length} Accessibility{' '} | ||
{axeErrors.length > 1 ? 'Errors' : 'Error'} | ||
</Text> | ||
), | ||
}, | ||
content: ( | ||
<ul style={{ padding: '0rem 0.7rem', listStyleType: 'none' }}> | ||
{axeErrors.map(error => ( | ||
<li>{error}</li> | ||
))} | ||
</ul> | ||
), | ||
}, | ||
]; | ||
|
||
return ( | ||
<div | ||
style={{ | ||
background: '#e3404022', | ||
}} | ||
> | ||
<Accordion panels={panels} /> | ||
</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
99 changes: 99 additions & 0 deletions
99
packages/fluentui/react-builder/src/components/MultiTypeKnob.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,99 @@ | ||
import * as React from 'react'; | ||
// import { isElement } from 'react-is'; | ||
// import * as _ from 'lodash'; | ||
// import * as FUI from '@fluentui/react-northstar'; | ||
// import * as FUIIcons from '@fluentui/react-icons-northstar'; | ||
|
||
/** | ||
* Displays a knob with the ability to switch between data `types`. | ||
*/ | ||
export const MultiTypeKnob: React.FunctionComponent<{ | ||
label: string; | ||
types: ('boolean' | 'number' | 'string' | 'literal')[]; | ||
value: any; | ||
onChange: (value: any) => void; | ||
onRemoveProp: () => void; | ||
options: string[]; | ||
required: boolean; | ||
}> = ({ label, types, value, onChange, onRemoveProp, options, required }) => { | ||
const defaultType = types[0]; | ||
const [type, setType] = React.useState(defaultType); | ||
|
||
const knob = knobs[type]; | ||
const handleChangeType = React.useCallback( | ||
e => setType(e.target.value), // @ts-ignore | ||
[], | ||
); | ||
|
||
const propId = `prop-${label}`; | ||
|
||
return ( | ||
<div style={{ paddingBottom: '4px', marginBottom: '4px', opacity: knob ? 1 : 0.4 }}> | ||
<div> | ||
{type !== 'boolean' && <label htmlFor={propId}>{label} </label>} | ||
{types.length === 1 ? ( | ||
<code style={{ float: 'right' }}>{type}</code> | ||
) : ( | ||
types.map(t => ( | ||
<button key={t} onClick={() => handleChangeType(t)}> | ||
{t} | ||
</button> | ||
)) | ||
)} | ||
</div> | ||
{knob && knob({ options, value, onChange, id: propId })} | ||
{type === 'boolean' && <label htmlFor={propId}> {label}</label>} | ||
{!required && type === 'literal' && value && ( | ||
<button | ||
style={{ | ||
background: 'none', | ||
border: '1px solid black', | ||
borderRadius: 4, | ||
margin: 8, | ||
}} | ||
aria-label="Remove" | ||
onClick={_ => { | ||
onRemoveProp(); | ||
}} | ||
> | ||
X | ||
</button> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export const knobs = { | ||
boolean: ({ value, onChange, id }) => ( | ||
<input id={id} type="checkbox" checked={!!value} onChange={e => onChange(!!e.target.checked)} /> | ||
), | ||
|
||
number: ({ value, onChange, id }) => ( | ||
<input | ||
id={id} | ||
style={{ width: '100%' }} | ||
type="number" | ||
value={Number(value)} | ||
onChange={e => onChange(Number(e.target.value))} | ||
/> | ||
), | ||
|
||
string: ({ value, onChange, id }) => ( | ||
<input id={id} style={{ width: '100%' }} value={String(value)} onChange={e => onChange(e.target.value)} /> | ||
), | ||
|
||
literal: ({ options, value, onChange, id }) => ( | ||
<select id={id} onChange={e => onChange(e.target.value)} value={value}> | ||
{options?.map(( | ||
opt, // FIXME the optional is workaround for showing `Dialog` props when selected from component tree | ||
) => ( | ||
<option key={opt} value={opt}> | ||
{opt} | ||
</option> | ||
))} | ||
</select> | ||
), | ||
|
||
ReactText: ({ value, onChange, id }) => knobs.string({ value, onChange, id }), | ||
'React.ElementType': ({ value, onChange, id }) => knobs.string({ value, onChange, id }), | ||
}; |
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,2 @@ | ||
export * from './useAxeOnElement'; | ||
export * from './useMode'; |
41 changes: 41 additions & 0 deletions
41
packages/fluentui/react-builder/src/hooks/useAxeOnElement.ts
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,41 @@ | ||
import * as React from 'react'; | ||
import * as axeCore from 'axe-core'; | ||
|
||
export function useAxeOnElement(): [any[], (selectedElementUuid: any) => void] { | ||
const [axeErrors, setAxeErrors] = React.useState([]); | ||
const runAxeOnElement = React.useCallback(selectedElementUuid => { | ||
const iframe = document.getElementsByTagName('iframe')[0]; | ||
const selectedComponentAxeErrors = []; | ||
axeCore.run( | ||
iframe, | ||
{ | ||
rules: { | ||
// excluding rules which are related to the whole page not to components | ||
'page-has-heading-one': { enabled: false }, | ||
region: { enabled: false }, | ||
'landmark-one-main': { enabled: false }, | ||
}, | ||
}, | ||
(err, result) => { | ||
if (err) { | ||
console.error('Axe failed', err); | ||
} else { | ||
result.violations.forEach(violation => { | ||
violation.nodes.forEach(node => { | ||
if (node.html.includes(`data-builder-id="${selectedElementUuid}"`)) { | ||
selectedComponentAxeErrors.push( | ||
node.failureSummary | ||
.replace('Fix all of the following:', '-') | ||
.replace('Fix any of the following:', '-'), | ||
); | ||
} | ||
}); | ||
}); | ||
} | ||
setAxeErrors(selectedComponentAxeErrors); | ||
}, | ||
); | ||
}, []); | ||
|
||
return [axeErrors, runAxeOnElement]; | ||
} |
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,13 @@ | ||
import * as React from 'react'; | ||
import { DesignerMode } from '../components/types'; | ||
|
||
export function useMode(): [ | ||
{ mode: DesignerMode; isExpanding: boolean; isSelecting: boolean }, | ||
(mode: DesignerMode) => void, | ||
] { | ||
const [mode, setMode] = React.useState<DesignerMode>('build'); | ||
const isExpanding = mode === 'build'; | ||
const isSelecting = mode === 'build' || mode === 'design'; | ||
|
||
return [{ mode, isExpanding, isSelecting }, setMode]; | ||
} |
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,2 @@ | ||
export * from './state'; | ||
export * from './utils'; |
Oops, something went wrong.