-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(cdk-experimental/ui-patterns): add popup behavior #31550
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a6625a1
feat(cdk-experimental/ui-patterns): add popup behavior
wagnermaciel c386241
fixup! feat(cdk-experimental/ui-patterns): add popup behavior
wagnermaciel ef7fa64
fixup! feat(cdk-experimental/ui-patterns): add popup behavior
wagnermaciel f929a9e
fixup! feat(cdk-experimental/ui-patterns): add popup behavior
wagnermaciel 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
30 changes: 30 additions & 0 deletions
30
src/cdk-experimental/ui-patterns/behaviors/popup/BUILD.bazel
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,30 @@ | ||
load("//tools:defaults.bzl", "ng_web_test_suite", "ts_project") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
ts_project( | ||
name = "popup", | ||
srcs = glob( | ||
["**/*.ts"], | ||
exclude = ["**/*.spec.ts"], | ||
), | ||
deps = [ | ||
"//:node_modules/@angular/core", | ||
"//src/cdk-experimental/ui-patterns/behaviors/signal-like", | ||
], | ||
) | ||
|
||
ts_project( | ||
name = "unit_test_sources", | ||
testonly = True, | ||
srcs = glob(["**/*.spec.ts"]), | ||
deps = [ | ||
":popup", | ||
"//:node_modules/@angular/core", | ||
], | ||
) | ||
|
||
ng_web_test_suite( | ||
name = "unit_tests", | ||
deps = [":unit_test_sources"], | ||
) |
76 changes: 76 additions & 0 deletions
76
src/cdk-experimental/ui-patterns/behaviors/popup/popup.spec.ts
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,76 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import {signal} from '@angular/core'; | ||
import {ComboboxPopupTypes, PopupControl, PopupControlInputs} from './popup'; | ||
|
||
type TestInputs = Partial<Pick<PopupControlInputs, 'popup' | 'expanded'>>; | ||
|
||
function getPopupControl(inputs: TestInputs = {}): PopupControl { | ||
const expanded = inputs.expanded || signal(false); | ||
const popup = signal({inert: signal(!expanded())}); | ||
const controls = signal('popup-element-id'); | ||
const hasPopup = signal(ComboboxPopupTypes.LISTBOX); | ||
|
||
return new PopupControl({ | ||
popup, | ||
controls, | ||
expanded, | ||
hasPopup, | ||
}); | ||
} | ||
|
||
describe('Popup Control', () => { | ||
describe('#open', () => { | ||
it('should set expanded to true and popup inert to false', () => { | ||
const control = getPopupControl(); | ||
|
||
expect(control.inputs.expanded()).toBeFalse(); | ||
expect(control.inputs.popup().inert()).toBeTrue(); | ||
|
||
control.open(); | ||
|
||
expect(control.inputs.expanded()).toBeTrue(); | ||
expect(control.inputs.popup().inert()).toBeFalse(); | ||
}); | ||
}); | ||
|
||
describe('#close', () => { | ||
it('should set expanded to false and popup inert to true', () => { | ||
const expanded = signal(true); | ||
const control = getPopupControl({expanded}); | ||
|
||
expect(control.inputs.expanded()).toBeTrue(); | ||
expect(control.inputs.popup().inert()).toBeFalse(); | ||
|
||
control.close(); | ||
|
||
expect(control.inputs.expanded()).toBeFalse(); | ||
expect(control.inputs.popup().inert()).toBeTrue(); | ||
}); | ||
}); | ||
|
||
describe('#toggle', () => { | ||
it('should toggle expanded and popup inert states', () => { | ||
const control = getPopupControl(); | ||
|
||
expect(control.inputs.expanded()).toBeFalse(); | ||
expect(control.inputs.popup().inert()).toBeTrue(); | ||
|
||
control.toggle(); | ||
|
||
expect(control.inputs.expanded()).toBeTrue(); | ||
expect(control.inputs.popup().inert()).toBeFalse(); | ||
|
||
control.toggle(); | ||
|
||
expect(control.inputs.expanded()).toBeFalse(); | ||
expect(control.inputs.popup().inert()).toBeTrue(); | ||
}); | ||
}); | ||
}); |
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,70 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import {SignalLike, WritableSignalLike} from '../signal-like/signal-like'; | ||
|
||
/** | ||
* Represents the inputs required for a popup behavior. | ||
* It includes a signal for the expanded state and a reference to the popup element. | ||
*/ | ||
export enum ComboboxPopupTypes { | ||
TREE = 'tree', | ||
GRID = 'grid', | ||
DIALOG = 'dialog', | ||
LISTBOX = 'listbox', | ||
} | ||
|
||
/** The element that serves as the popup. */ | ||
export interface Popup { | ||
/** Whether the popup is interactive or not. */ | ||
inert: WritableSignalLike<boolean>; | ||
ok7sai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** Represents the inputs for the PopupControl behavior. */ | ||
export interface PopupControlInputs { | ||
/** The element that serves as the popup. */ | ||
popup: SignalLike<Popup>; | ||
|
||
/* Refers to the element that serves as the popup. */ | ||
controls: SignalLike<string>; | ||
|
||
/* Whether the popup is open or closed. */ | ||
expanded: WritableSignalLike<boolean>; | ||
|
||
/* Corresponds to the popup type. */ | ||
hasPopup: SignalLike<ComboboxPopupTypes>; | ||
ok7sai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* A behavior that manages the open/close state of a component. | ||
* It provides methods to open, close, and toggle the state, | ||
* which is controlled via a writable signal. | ||
*/ | ||
export class PopupControl { | ||
/** The inputs for the popup behavior, containing the `expanded` state signal. */ | ||
constructor(readonly inputs: PopupControlInputs) {} | ||
|
||
/** Opens the popup by setting the expanded state to true. */ | ||
open(): void { | ||
this.inputs.expanded.set(true); | ||
this.inputs.popup().inert.set(false); | ||
} | ||
|
||
/** Closes the popup by setting the expanded state to false. */ | ||
close(): void { | ||
this.inputs.expanded.set(false); | ||
this.inputs.popup().inert.set(true); | ||
} | ||
|
||
/** Toggles the popup's expanded state. */ | ||
toggle(): void { | ||
const expanded = !this.inputs.expanded(); | ||
this.inputs.expanded.set(expanded); | ||
this.inputs.popup().inert.set(!expanded); | ||
} | ||
} |
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.