Skip to content
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

[NTP Next] Add updated NTP with background support #26408

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ const slashStoriesIndexer: Indexer = {
const config: StorybookConfig = {
stories: process.env.STORYBOOK_STORYPATH
? [`../${process.env.STORYBOOK_STORYPATH}`]
: ['../components/**/stories/*.tsx', '../components/**/*.stories.tsx'],
: [
'../components/**/stories/*.tsx',
'../components/**/*.stories.tsx',
'../browser/resources/**/stories/*.tsx'
],
typescript: {
check: false,
reactDocgen: false,
Expand Down
7 changes: 7 additions & 0 deletions browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,13 @@
kOsDesktop, \
FEATURE_VALUE_TYPE(features::kBraveNtpSearchWidget), \
}, \
{ \
"brave-use-updated-ntp", \
"Use the updated New Tab Page", \
"Uses an updated version of the New Tab Page", \
kOsDesktop, \
FEATURE_VALUE_TYPE(features::kUseUpdatedNTP), \
}, \
{ \
"brave-adblock-cname-uncloaking", \
"Enable CNAME uncloaking", \
Expand Down
4 changes: 4 additions & 0 deletions browser/brave_browser_features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

namespace features {

BASE_FEATURE(kUseUpdatedNTP,
"BraveUseUpdatedNewTabPage",
base::FEATURE_DISABLED_BY_DEFAULT);

// Cleanup Session Cookies on browser restart if Session Restore is enabled.
BASE_FEATURE(kBraveCleanupSessionCookiesOnSessionRestore,
"BraveCleanupSessionCookiesOnSessionRestore",
Expand Down
1 change: 1 addition & 0 deletions browser/brave_browser_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace features {

BASE_DECLARE_FEATURE(kUseUpdatedNTP);
BASE_DECLARE_FEATURE(kBraveCleanupSessionCookiesOnSessionRestore);
BASE_DECLARE_FEATURE(kBraveCopyCleanLinkByDefault);
BASE_DECLARE_FEATURE(kBraveCopyCleanLinkFromJs);
Expand Down
4 changes: 4 additions & 0 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ using extensions::ChromeContentBrowserClientExtensionsPart;
#if !BUILDFLAG(IS_ANDROID)
#include "brave/browser/new_tab/new_tab_shows_navigation_throttle.h"
#include "brave/browser/ui/geolocation/brave_geolocation_permission_tab_helper.h"
#include "brave/browser/ui/webui/brave_new_tab/new_tab_page_ui.h"
#include "brave/browser/ui/webui/brave_news_internals/brave_news_internals_ui.h"
#include "brave/browser/ui/webui/brave_rewards/rewards_page_top_ui.h"
#include "brave/browser/ui/webui/brave_rewards/rewards_panel_ui.h"
Expand Down Expand Up @@ -867,6 +868,9 @@ void BraveContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
content::RegisterWebUIControllerInterfaceBinder<
commands::mojom::CommandsService, BraveSettingsUI>(map);
}
content::RegisterWebUIControllerInterfaceBinder<
brave_new_tab::mojom::NewTabPageHandler, brave_new_tab::NewTabPageUI>(
map);
#endif

auto* prefs =
Expand Down
25 changes: 25 additions & 0 deletions browser/resources/brave_new_tab/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2025 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

import("//brave/components/common/typescript.gni")
import("//mojo/public/tools/bindings/mojom.gni")

assert(!is_android)

transpile_web_ui("resources") {
entry_points = [ [
"new_tab",
rebase_path("new_tab_page.tsx"),
] ]
resource_name = "brave_new_tab"
output_module = true
deps = [ "//brave/browser/ui/webui/brave_new_tab:mojom_js" ]
}

pack_web_resources("generated_resources") {
resource_name = "brave_new_tab"
output_dir = "$root_gen_dir/brave/browser/resources/brave_new_tab"
deps = [ ":resources" ]
}
94 changes: 94 additions & 0 deletions browser/resources/brave_new_tab/components/app.style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* Copyright (c) 2025 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

import { color, font } from '@brave/leo/tokens/css/variables'
import { scoped, global } from '../lib/scoped_css'

export const style = scoped.css`

.settings {
--leo-icon-size: 20px;

position: absolute;
inset-block-start: 4px;
inset-inline-end: 4px;
block-size: 20px;
inline-size: 20px;
opacity: 0.5;
color: #fff;
filter: drop-shadow(0px 1px 4px rgba(0, 0, 0, 0.60));

&:hover {
opacity: 0.7;
cursor: pointer;
}
}

main {
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
padding-top: 40px;
}

.topsites-container {
min-height: 32px;
}

.searchbox-container {
flex: 1 1 auto;
margin: 16px 0;
}

.background-caption-container {
margin: 8px 0;
}

.widget-container {
min-height: 8px;
}

`

global.css`
@scope (${style.selector}) {

& {
font: ${font.default.regular};
color: ${color.text.primary};
}

button {
margin: 0;
padding: 0;
background: 0;
border: none;
text-align: unset;
width: unset;
font: inherit;
cursor: pointer;

&:disabled {
cursor: default;
}
}

h2 {
font: ${font.heading.h2};
margin: 0;
}

h3 {
font: ${font.heading.h3};
margin: 0;
}

h4 {
font: ${font.heading.h4};
margin: 0;
}
}
`
43 changes: 43 additions & 0 deletions browser/resources/brave_new_tab/components/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Copyright (c) 2025 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

import * as React from 'react'
import Icon from '@brave/leo/react/icon'

import { Background } from './background'
import { BackgroundCaption } from './background_caption'
import { SettingsModal, SettingsView } from './settings/settings_modal'

import { style } from './app.style'

export function App() {
const [settingsView, setSettingsView] =
React.useState<SettingsView | null>(null)

return (
<div data-css-scope={style.scope}>
<button
className='settings'
onClick={() => setSettingsView('background')}
>
<Icon name='settings' />
</button>
<main>
<div className='topsites-container' />
<div className='searchbox-container' />
<div className='background-caption-container'>
<BackgroundCaption />
</div>
<div className='widget-container' />
</main>
<Background />
<SettingsModal
isOpen={Boolean(settingsView)}
initialView={settingsView}
onClose={() => setSettingsView(null)}
/>
</div>
)
}
47 changes: 47 additions & 0 deletions browser/resources/brave_new_tab/components/background.style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright (c) 2025 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

import { scoped } from '../lib/scoped_css'

export const style = scoped.css`

& {
pointer-events: none;
position: fixed;
inset: 0;
z-index: -1;
display: flex;
animation-name: fade-in;
animation-timing-function: ease-in-out;
animation-duration: 350ms;
animation-delay: 0s;
animation-fill-mode: both;

> div {
flex: 1 1 auto;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
}
}

.image-background {
background:
linear-gradient(
rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0) 35%, rgba(0, 0, 0, 0) 80%,
rgba(0, 0, 0, 0.6) 100%),
var(--ntp-background);
}

.color-background {
background: var(--ntp-background);
}

@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}

`
68 changes: 68 additions & 0 deletions browser/resources/brave_new_tab/components/background.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Copyright (c) 2025 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

import * as React from 'react'

import { useNewTabState } from './new_tab_context'
import { loadImage } from '../lib/image_loader'

import { style } from './background.style'

function setBackgroundVariable(value: string) {
if (value) {
document.body.style.setProperty('--ntp-background', value)
} else {
document.body.style.removeProperty('--ntp-background')
}
}

function ImageBackground(props: { url: string }) {
// In order to avoid a "flash-of-unloaded-image", load the image in the
// background and only update the background CSS variable when the image has
// finished loading.
React.useEffect(() => {
loadImage(props.url).then((loaded) => {
if (loaded) {
setBackgroundVariable(`url(${CSS.escape(props.url)})`)
}
})
}, [props.url])

return <div className='image-background' />
}

function ColorBackground(props: { colorValue: string }) {
React.useEffect(() => {
setBackgroundVariable(props.colorValue)
}, [props.colorValue])

return <div className='color-background' />
}

export function Background() {
const currentBackground = useNewTabState((state) => state.currentBackground)

function renderBackground() {
if (!currentBackground) {
return <ColorBackground colorValue='transparent' />
}

switch (currentBackground.type) {
case 'brave':
case 'custom':
case 'sponsored':
return <ImageBackground url={currentBackground.imageUrl} />
case 'solid':
case 'gradient':
return <ColorBackground colorValue={currentBackground.cssValue} />
}
}

return (
<div data-css-scope={style.scope}>
{renderBackground()}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright (c) 2025 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

import { color, font } from '@brave/leo/tokens/css/variables'
import { scoped } from '../lib/scoped_css'

export const style = scoped.css`
a {
text-decoration: none;
color: inherit;
}

.photo-credits {
color: ${color.white};
font: ${font.xSmall.regular};
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.10);
opacity: .5;
}

.sponsored-logo {
--leo-icon-size: 20px;

display: flex;
flex-direction: column;
align-items: end;
color: ${color.white};

leo-icon {
opacity: 0;
transition: opacity 200ms;
}

img {
margin: 2px 20px 0 20px;
width: 170px;
height: auto;
}

&:hover {
leo-icon {
opacity: .7;
}
}
}
`
Loading
Loading