If you want to dismiss the picker when "Cancel" button is tapped, you can observe changes to $selected binding and check if it is empty.
Observing the changes like below doesn't get triggered when Cancel is tapped:
PhotosPicker(
selection: $viewModel.selection,
maxSelectionCount: 1,
selectionBehavior: .default,
matching: .images,
preferredItemEncoding: .current,
photoLibrary: .shared()
) {
Text("Select Photos")
}
.photosPickerStyle(.inline)
.photosPickerAccessoryVisibility(.hidden, edges: .bottom)
.onChange(of: viewModel.selection, { oldValue, newValue in
print(oldValue)
print(newValue)
})