Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove KeytipData usage from Link, Checkbox, Toggle (react-next) (#13742
) * export useKeytipData * remove KeytipData usage from Checkbox, add withKeytipData HOC * remove KeytipData for toggle * Remove keytipData for link * update example * cleanups * Change files * resolve comments * avoid adding keytipData prop * fixes * fix keytip set to invisible if addKeytipQueue is called multiple time * resolve comment
- Loading branch information
Showing
32 changed files
with
640 additions
and
113 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
change/@fluentui-react-next-2020-06-22-11-18-19-xgao-useKeytipData.json
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,8 @@ | ||
{ | ||
"type": "prerelease", | ||
"comment": "Remove KeytipData for Checkbox, Toggle, and Link.", | ||
"packageName": "@fluentui/react-next", | ||
"email": "xgao@microsoft.com", | ||
"dependentChangeType": "patch", | ||
"date": "2020-06-22T18:18:19.664Z" | ||
} |
8 changes: 8 additions & 0 deletions
8
change/office-ui-fabric-react-2020-06-22-11-18-19-xgao-useKeytipData.json
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,8 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "Keytip: add useKeytipRef hook.", | ||
"packageName": "office-ui-fabric-react", | ||
"email": "xgao@microsoft.com", | ||
"dependentChangeType": "patch", | ||
"date": "2020-06-22T18:18:04.940Z" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,5 @@ export interface IKeytipDataProps { | |
*/ | ||
disabled?: boolean; | ||
} | ||
|
||
export type KeytipDataOptions = IKeytipDataProps; |
2 changes: 2 additions & 0 deletions
2
packages/office-ui-fabric-react/src/components/KeytipData/index.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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './KeytipData'; | ||
export { KeytipDataOptions } from './KeytipData.types'; | ||
export * from './useKeytipRef'; |
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
53 changes: 53 additions & 0 deletions
53
packages/office-ui-fabric-react/src/components/KeytipData/useKeytipRef.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,53 @@ | ||
import * as React from 'react'; | ||
import { KeytipDataOptions } from './KeytipData.types'; | ||
import { DATAKTP_TARGET, DATAKTP_EXECUTE_TARGET, DATAKTP_ARIA_TARGET } from '../../utilities/keytips/index'; | ||
import { useKeytipData } from './useKeytipData'; | ||
|
||
/** | ||
* Hook that creates a ref which is used for passing to Keytip target element. | ||
* The ref will handle setting the attributes needed for Keytip to work. | ||
*/ | ||
export function useKeytipRef<TElement extends HTMLElement = HTMLElement>( | ||
options: KeytipDataOptions, | ||
): React.Ref<TElement> { | ||
const { keytipId, ariaDescribedBy } = useKeytipData(options); | ||
|
||
const contentRef: React.Ref<TElement> = (contentElement: TElement | null): void => { | ||
if (!contentElement) { | ||
return; | ||
} | ||
|
||
const targetElement = findFirstElement(contentElement, DATAKTP_TARGET) || contentElement; | ||
const executeElement = findFirstElement(contentElement, DATAKTP_EXECUTE_TARGET) || targetElement; | ||
const ariaElement = findFirstElement(contentElement, DATAKTP_ARIA_TARGET) || executeElement; | ||
|
||
setAttribute(targetElement, DATAKTP_TARGET, keytipId); | ||
setAttribute(executeElement, DATAKTP_EXECUTE_TARGET, keytipId); | ||
setAttribute(ariaElement, 'aria-describedby', ariaDescribedBy, true); | ||
}; | ||
|
||
return contentRef; | ||
} | ||
|
||
function setAttribute( | ||
element: HTMLElement | null, | ||
attributeName: string, | ||
attributeValue: string | undefined, | ||
append: boolean = false, | ||
): void { | ||
if (element && attributeValue) { | ||
let value = attributeValue; | ||
if (append) { | ||
const currentValue = element.getAttribute(attributeName); | ||
if (currentValue && currentValue.indexOf(attributeName) === -1) { | ||
value = `${currentValue} ${attributeValue}`; | ||
} | ||
} | ||
|
||
element.setAttribute(attributeName, value); | ||
} | ||
} | ||
|
||
function findFirstElement(rootElement: HTMLElement, dataAttribute: string): HTMLElement | null { | ||
return rootElement.querySelector(`[${dataAttribute}]`); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from 'office-ui-fabric-react/lib/Keytip'; | ||
export * from './components/Keytip/index'; |
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.