Repository navigation
feat: Add ref forwarding via nativeAttributes #5049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mgmolisani
wants to merge
7
commits into
cloudscape-design:main
Choose a base branch
from
mgmolisani:feat/native-element-refs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9950e19
feat: Add ref forwarding via nativeAttributes
43d7c1b
docs: Document ref support on nativeAttributes
3626722
test: Rework the native refs page as a test page
f557cc5
fix: Type table cell attributes as element props
ff86d10
make native refs infer element type
pan-kot 328948b
refactor
pan-kot a9f0d02
fix item card
pan-kot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,225 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'; | ||
|
|
||
| import Box from '~components/box'; | ||
| import Button from '~components/button'; | ||
| import ColumnLayout from '~components/column-layout'; | ||
| import Container from '~components/container'; | ||
| import Header from '~components/header'; | ||
| import KeyValuePairs from '~components/key-value-pairs'; | ||
| import SpaceBetween from '~components/space-between'; | ||
| import StatusIndicator, { StatusIndicatorProps } from '~components/status-indicator'; | ||
| import Textarea, { TextareaProps } from '~components/textarea'; | ||
|
|
||
| import { SimplePage } from '../app/templates'; | ||
|
|
||
| const INSERTED = `[inserted]`; | ||
|
|
||
| function getVisibility(ratio: number | null): { type: StatusIndicatorProps.Type; label: string } { | ||
| return ratio === null | ||
| ? { type: `pending`, label: `Not measured` } | ||
| : ratio === 0 | ||
| ? { type: `error`, label: `Not visible` } | ||
| : ratio < 1 | ||
| ? { type: `warning`, label: `Partially visible` } | ||
| : { type: `success`, label: `Fully visible` }; | ||
| } | ||
|
|
||
| export default function NativeRefsPage() { | ||
| const componentRef = useRef<TextareaProps.Ref>(null); | ||
| const nativeRef = useRef<HTMLTextAreaElement | null>(null); | ||
| const containerRef = useRef<HTMLDivElement | null>(null); | ||
|
|
||
| const [value, setValue] = useState(`Select or place the cursor in this text. Drag the corner to resize.`); | ||
| const [nativeEdit, setNativeEdit] = useState<{ | ||
| readonly caret: number; | ||
| readonly inputType: string; | ||
| readonly data: string | null; | ||
| } | null>(null); | ||
| const [size, setSize] = useState<{ width: number; height: number } | null>(null); | ||
| const [visibleRatio, setVisibleRatio] = useState<number | null>(null); | ||
| const [pendingCursor, setPendingCursor] = useState<number | null>(null); | ||
|
|
||
| useLayoutEffect(() => { | ||
| nativeRef.current?.scrollIntoView({ behavior: `smooth`, block: `center` }); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| const container = containerRef.current; | ||
| const element = nativeRef.current; | ||
|
|
||
| if (container && element) { | ||
| const observer = new IntersectionObserver( | ||
| entries => { | ||
| setVisibleRatio(entries[0].intersectionRatio); | ||
| }, | ||
| { root: container, threshold: [0, 0.25, 0.5, 0.75, 1] } | ||
| ); | ||
| observer.observe(element); | ||
|
|
||
| return () => { | ||
| observer.disconnect(); | ||
| }; | ||
| } | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| const element = nativeRef.current; | ||
|
|
||
| if (element) { | ||
| const observer = new ResizeObserver(entries => { | ||
| const { width, height } = entries[0].contentRect; | ||
|
|
||
| setSize({ width: Math.round(width), height: Math.round(height) }); | ||
| }); | ||
|
|
||
| observer.observe(element); | ||
|
|
||
| return () => { | ||
| observer.disconnect(); | ||
| }; | ||
| } | ||
| }, []); | ||
|
|
||
| useLayoutEffect(() => { | ||
| if (pendingCursor !== null) { | ||
| nativeRef.current?.setSelectionRange(pendingCursor, pendingCursor); | ||
| nativeRef.current?.focus(); | ||
|
|
||
| setPendingCursor(null); | ||
| } | ||
| }, [pendingCursor]); | ||
|
|
||
| const handleChange: TextareaProps['onChange'] = event => { | ||
| setValue(event.detail.value); | ||
| }; | ||
|
|
||
| const handleNativeChange = (event: React.ChangeEvent<HTMLTextAreaElement>): void => { | ||
| const inputEvent = event.nativeEvent as InputEvent; | ||
| setNativeEdit({ | ||
| caret: event.currentTarget.selectionStart, | ||
| inputType: inputEvent.inputType ?? `unknown`, | ||
| data: inputEvent.data, | ||
| }); | ||
| }; | ||
|
|
||
| const focusViaComponentRef = (): void => { | ||
| componentRef.current?.focus(); | ||
| }; | ||
|
|
||
| const focusViaNativeRef = (): void => { | ||
| nativeRef.current?.focus(); | ||
| }; | ||
|
|
||
| const insertViaNativeRef = (): void => { | ||
| const element = nativeRef.current; | ||
| if (element) { | ||
| const start = element.selectionStart ?? 0; | ||
| const end = element.selectionEnd ?? 0; | ||
|
|
||
| setValue(value.slice(0, start) + INSERTED + value.slice(end)); | ||
| setPendingCursor(start + INSERTED.length); | ||
| } | ||
| }; | ||
|
|
||
| const scrollViaNativeRef = (): void => { | ||
| nativeRef.current?.scrollIntoView({ behavior: `smooth`, block: `center` }); | ||
| }; | ||
|
|
||
| const visibility = getVisibility(visibleRatio); | ||
| const ratioLabel = visibleRatio === null ? `-` : `${Math.round(visibleRatio * 100)}%`; | ||
|
|
||
| return ( | ||
| <SimplePage title="Native element refs"> | ||
| <SpaceBetween size={`l`}> | ||
| <div> | ||
| <Container variant="stacked" disableContentPaddings={true}> | ||
| <div ref={containerRef} style={{ blockSize: 220, overflowY: `auto`, overflowX: `clip`, padding: 16 }}> | ||
| <div style={{ blockSize: 240 }} /> | ||
| <Textarea | ||
| ref={componentRef} | ||
| ariaLabel={`Native refs test textarea`} | ||
| nativeTextareaAttributes={{ ref: nativeRef, onChange: handleNativeChange }} | ||
| value={value} | ||
| onChange={handleChange} | ||
| /> | ||
| <div style={{ blockSize: 240 }} /> | ||
| </div> | ||
| </Container> | ||
|
|
||
| <Container variant="stacked"> | ||
| <ColumnLayout columns={2} variant="text-grid"> | ||
| <SpaceBetween size={`xxs`}> | ||
| <Box variant="awsui-key-label">Component ref</Box> | ||
| <SpaceBetween size={`xs`} direction={`horizontal`}> | ||
| <Button onClick={focusViaComponentRef}>{`Focus`}</Button> | ||
| </SpaceBetween> | ||
| </SpaceBetween> | ||
|
|
||
| <SpaceBetween size={`xxs`}> | ||
| <Box variant="awsui-key-label">Native ref</Box> | ||
| <SpaceBetween size={`xs`} direction={`horizontal`}> | ||
| <Button onClick={focusViaNativeRef}>{`Focus`}</Button> | ||
| <Button onClick={insertViaNativeRef}>{`Insert at selection`}</Button> | ||
| <Button onClick={scrollViaNativeRef}>{`Scroll into view`}</Button> | ||
| </SpaceBetween> | ||
| </SpaceBetween> | ||
| </ColumnLayout> | ||
| </Container> | ||
| </div> | ||
|
|
||
| <Container> | ||
| <ColumnLayout columns={2} variant="text-grid"> | ||
| <SpaceBetween size={`m`}> | ||
| <Header | ||
| variant="h2" | ||
| description="Both handlers are chained: the component's onChange manages the controlled value, while the chained native handler reports fields the event detail does not carry." | ||
| > | ||
| {`Native event`} | ||
| </Header> | ||
| <KeyValuePairs | ||
| columns={3} | ||
| items={[ | ||
| { label: `inputType`, value: nativeEdit ? nativeEdit.inputType : `-` }, | ||
| { label: `data`, value: nativeEdit ? JSON.stringify(nativeEdit.data) : `-` }, | ||
| { label: `selectionStart`, value: nativeEdit ? nativeEdit.caret : `-` }, | ||
| ]} | ||
| /> | ||
| </SpaceBetween> | ||
|
|
||
| <SpaceBetween size={`m`}> | ||
| <Header variant="h2" description="Observers attached directly to the native element."> | ||
| {`Observers`} | ||
| </Header> | ||
| <KeyValuePairs | ||
| columns={2} | ||
| items={[ | ||
| { | ||
| type: `group`, | ||
| title: `ResizeObserver`, | ||
| items: [ | ||
| { label: `Width`, value: size ? size.width : `-` }, | ||
| { label: `Height`, value: size ? size.height : `-` }, | ||
| ], | ||
| }, | ||
| { | ||
| type: `group`, | ||
| title: `IntersectionObserver`, | ||
| items: [ | ||
| { | ||
| label: `Visible in container`, | ||
| value: <StatusIndicator type={visibility.type}>{visibility.label}</StatusIndicator>, | ||
| }, | ||
| { label: `Visible ratio`, value: ratioLabel }, | ||
| ], | ||
| }, | ||
| ]} | ||
| /> | ||
| </SpaceBetween> | ||
| </ColumnLayout> | ||
| </Container> | ||
| </SpaceBetween> | ||
| </SimplePage> | ||
| ); | ||
| } | ||
|
pan-kot marked this conversation as resolved.
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.